Handling Exceptions With PL/SQL
Handling Exceptions With PL/SQL
• What is an exception?
– Identifier in PL/SQL that is raised during
execution
• How is it raised?
– An Oracle error occurs.
– You raise it explicitly.
• How do you handle it?
– Trap it with a handler.
– Propagate it to the calling environment.
1
Handling Exceptions
BEGIN BEGIN
Exception Exception
is raised is raised
EXCEPTION EXCEPTION
Exception Exception is
is trapped END; END; not trapped
Exception
propagates to calling
environment
2
Exception Types
3
Trapping Exceptions
Syntax
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]
4
Trapping Exceptions Guidelines
5
Trapping Predefined
Oracle Server Errors
• Reference the standard name in the
exception-handling routine.
• Sample predefined exceptions:
– NO_DATA_FOUND
– TOO_MANY_ROWS
– INVALID_CURSOR
– ZERO_DIVIDE
– DUP_VAL_ON_INDEX
6
Predefined Exception
Syntax
BEGIN SELECT ... COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
statement1;
statement2;
WHEN TOO_MANY_ROWS THEN
statement1;
WHEN OTHERS THEN
statement1;
statement2;
statement3;
END;
7
Trapping Non-Predefined Oracle
Server Errors
8
Non-Predefined Error
Trap for Oracle Server error number
–2292, an integrity constraint violation.
DECLARE
e_emps_remaining EXCEPTION;
e_emps_remaining EXCEPTION; 1
PRAGMA
PRAGMA EXCEPTION_INIT
EXCEPTION_INIT ((
e_emps_remaining,-2292);
e_emps_remaining, -2292); 2
v_deptno dept.deptno%TYPE := &p_deptno;
BEGIN
DELETE FROM dept
WHERE deptno = v_deptno;
COMMIT;
EXCEPTION
WHEN e_emps_remaining THEN 3
DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' ||
TO_CHAR(v_deptno) || '. Employees exist. ');
END;
9
Trapping User-Defined
Exceptions
10
User-Defined Exception
Example
DECLARE
EXCEPTION;
e_invalid_product EXCEPTION; 1
BEGIN
UPDATE product
SET descrip = '&product_description'
WHERE prodid = &product_number;
IF SQL%NOTFOUND THEN
RAISE e_invalid_product; 2
END IF;
COMMIT;
EXCEPTION
WHEN e_invalid_product
e_invalid_product THEN 3
DBMS_OUTPUT.PUT_LINE('Invalid product number.');
END;
11
Functions for Trapping
Exceptions
• SQLCODE
Returns the numeric value for the error
code
• SQLERRM
Returns the message associated with the
error number
12
Functions for Trapping Exceptions
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;
13
Calling Environments
SQL*Plus Displays error number and message
to screen
Sql Displays error number and message
Developer
to screen
Accesses error number and message
Oracle in a trigger by means of the
Developer ERROR_CODE and ERROR_TEXT
Forms packaged functions
Precompiler Accesses exception number through
application the SQLCA data structure
15
RAISE_APPLICATION_ERROR
Procedure
Syntax
raise_application_error (error_number,
message[, {TRUE | FALSE}]);
16
RAISE_APPLICATION_ERROR
Procedure
• Used in two different places:
– Executable section
– Exception section
• Returns error conditions to the user in a
manner consistent with other Oracle
Server errors
17