Exceptions - PPT 0
Exceptions - PPT 0
Handling Exceptions
DECLARE DECLARE
BEGIN BEGIN
Exception Exception
is raised is raised
EXCEPTION EXCEPTION
Exception Exception
is trapped END; END; is not
trapped
Exception
propagates to calling
environment
}
• Predefined Oracle Server Implicitly
• Nonpredefined Oracle Server raised
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]
Syntax:
BEGIN
. . .
EXCEPTION
WHEN NO_DATA_FOUND THEN
statement1;
statement2;
DEFINE p_deptno = 10 1
DECLARE
e_emps_remaining EXCEPTION; 2
PRAGMA EXCEPTION_INIT
(e_emps_remaining, -2292);
BEGIN
DELETE FROM departments
WHERE department_id = &p_deptno;
COMMIT;
EXCEPTION 3
WHEN e_emps_remaining THEN
DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' ||
TO_CHAR(&p_deptno) || '. Employees exist. ');
END;
Example:
DECLARE
v_error_code NUMBER;
v_error_message VARCHAR2(255);
BEGIN
...
EXCEPTION
...
WHEN OTHERS THEN
ROLLBACK;
v_error_code := SQLCODE ;
v_error_message := SQLERRM ;
INSERT INTO errors
VALUES(v_error_code, v_error_message);
END;
Syntax:
raise_application_error (error_number,
message);
Exception section:
...
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20201,
'Manager is not a valid employee.');
END;