ExceptionHandling (PLSQL)
ExceptionHandling (PLSQL)
Ex:
BEGIN
DBMS_OUTPUT.PUT_LINE(TENAME||','||TSAL);
EXCEPTION
END;
/
Too_many_rows: When SELECT…..INTO clause try to return more
than one value or one row then oracle server returns an error.
EX: ORA-1422: Exact fetch returns more than requested number of rows.
EX:
BEGIN
DBMS_OUTPUT.PUT_LINE(TSAL);
EXCEPTION
END;
EX:
BEGIN
X:=&X;
Y:=&Y;
Z:=X/Y;
DBMS_OUTPUT.PUT_LINE('RESULT:-'||Z);
EXCEPTION
END;
/
Invalid cursor: When we are not opening the cursor but we are try to
perform operations on cursor then ORACLE returns an error.
EX:
declare
begin
close c1;
exception
/
Cursor_already_open: Before reopening the cursor we must close the
cursor properly otherwise ORACLE returns an error i.e.
EX:
declare
begin
open c1;
loop
end loop;
open c1;
exception
end;
SQLCODE & SQLERRM: PL/SQL provides following built-in
properties which are used in error handling.
SQLCODE returns error code.
SQLERRM returns error message.
Ex:
DECLARE
X NUMBER(10);
Y NUMBER(20);
Z NUMBER(10);
BEGIN
X:=&X;
Y:=&Y;
Z:=X/Y;
DBMS_OUTPUT.PUT_LINE(Z);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLCODE);
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
OUTPUT:
Enter value for x: 10
Enter value for y: 2
5
Enter value for x: 10
Enter value for y: 0
-1476---------error code
ORA-01476: divisor is equal to zero------error message