In PL/SQL, Exceptions are errors at Runtime.
In PL/SQL Block, If any errors occured at compile time then Oracle will take care.
But Compiler could not find all errors at compile time like ZERO_DIVIDE, NO_DATA_FOUND, TOO_MANY_ROWS, VALUE_ERROR etc..
To Handle Exceptions the following syntax will follow in PL/SQL
DECLARE
--DECLARATION SECTION
BEGIN
--BUSINESS LOGIC
EXCEPTION
WHEN EXCEPTION_NAME THEN
--EXCEPTION SECTION
END
Example:
DECLARE
V_ENAME EMP.ENAME%TYPE;-- It is the Type Declaration of Ename in Emp table
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP WHERE EMPNO=123;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NOT FOUND');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('UNKNOW EXCEPTION');
END;
Note: Here in the above section the NO_DATA_FOUND exception will handle at run time. If the error is not known then OTHERS exception type will handle. And it should be down of all exceptions otherwise compile time error occured.
1) THROWING ERROR BY USER
Using the RAISE statement user can raise the exception if required.
Example:
BEGIN
RAISE; -- OR RAISE NO_DATA_FOUND;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO_DATA_FOUND EXCEPTION');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('UNKNOW EXCEPTION');
END;
2) USER DEFINED EXCEPTION
In PL/SQL, User can create the Exception and can handle at run time as follows.
DECLARE
USER_EXCEPTION EXCEPTION;
BEGIN
RAISE USER_EXCEPTION;
EXCEPTION
WHEN USER_EXCEPTION THEN
DBMS_OUTPUT.PUT_LINE('USER_EXCEPTION');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('UNKNOW EXCEPTION');
END;
In PL/SQL Block, If any errors occured at compile time then Oracle will take care.
But Compiler could not find all errors at compile time like ZERO_DIVIDE, NO_DATA_FOUND, TOO_MANY_ROWS, VALUE_ERROR etc..
To Handle Exceptions the following syntax will follow in PL/SQL
DECLARE
--DECLARATION SECTION
BEGIN
--BUSINESS LOGIC
EXCEPTION
WHEN EXCEPTION_NAME THEN
--EXCEPTION SECTION
END
Example:
DECLARE
V_ENAME EMP.ENAME%TYPE;-- It is the Type Declaration of Ename in Emp table
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP WHERE EMPNO=123;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NOT FOUND');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('UNKNOW EXCEPTION');
END;
Note: Here in the above section the NO_DATA_FOUND exception will handle at run time. If the error is not known then OTHERS exception type will handle. And it should be down of all exceptions otherwise compile time error occured.
1) THROWING ERROR BY USER
Using the RAISE statement user can raise the exception if required.
Example:
BEGIN
RAISE; -- OR RAISE NO_DATA_FOUND;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO_DATA_FOUND EXCEPTION');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('UNKNOW EXCEPTION');
END;
2) USER DEFINED EXCEPTION
In PL/SQL, User can create the Exception and can handle at run time as follows.
DECLARE
USER_EXCEPTION EXCEPTION;
BEGIN
RAISE USER_EXCEPTION;
EXCEPTION
WHEN USER_EXCEPTION THEN
DBMS_OUTPUT.PUT_LINE('USER_EXCEPTION');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('UNKNOW EXCEPTION');
END;
No comments:
Post a Comment