0% found this document useful (0 votes)
33 views

Handling Exceptions With PL/SQL

The document discusses handling exceptions in PL/SQL. It defines exceptions as identifiers raised during execution, such as Oracle errors or those explicitly raised. Exceptions can be trapped with handlers and propagated to the calling environment. The document outlines how to trap predefined Oracle exceptions, user-defined exceptions, and non-predefined Oracle errors. Functions like SQLCODE and SQLERRM can retrieve error details. Exceptions can be propagated from nested blocks or raised with RAISE_APPLICATION_ERROR.

Uploaded by

Edimarf Satumbo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Handling Exceptions With PL/SQL

The document discusses handling exceptions in PL/SQL. It defines exceptions as identifiers raised during execution, such as Oracle errors or those explicitly raised. Exceptions can be trapped with handlers and propagated to the calling environment. The document outlines how to trap predefined Oracle exceptions, user-defined exceptions, and non-predefined Oracle errors. Functions like SQLCODE and SQLERRM can retrieve error details. Exceptions can be propagated from nested blocks or raised with RAISE_APPLICATION_ERROR.

Uploaded by

Edimarf Satumbo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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

Trap the exception Propagate the exception


DECLARE DECLARE

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

• Predefined Oracle Server


• Non-predefined Oracle Server } Implicitly
raised

• User-defined Explicitly raised

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

• WHEN OTHERS is the last clause.


• EXCEPTION keyword starts exception-
handling section.
• Several exception handlers are allowed.
• Only one handler is processed before
leaving the block.

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

Declare Associate Reference

Declarative section Exception-handling


section

• Name the • Code the PRAGMA • Handle the


exception EXCEPTION_INIT raised
exception

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

Declare Raise Reference

Declarative Executable Exception-handling


section section section

• Name the • Explicitly raise • Handle the


exception the exception by raised
using the RAISE exception
statement

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

An enclosing Traps exception in exception-


PL/SQL block handling routine of enclosing block
14
Propagating Exceptions
DECLARE
. . .
e_no_rows exception;
e_integrity exception;
PRAGMA EXCEPTION_INIT (e_integrity, -2292);
BEGIN
FOR c_record IN emp_cursor LOOP
BEGIN
SELECT ...
Subblocks can handle UPDATE ...
IF SQL%NOTFOUND THEN
an exception or pass RAISE e_no_rows;
END IF;
the exception to the EXCEPTION
WHEN e_integrity THEN ...
enclosing block. WHEN e_no_rows THEN ...
END;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN . . .
WHEN TOO_MANY_ROWS THEN . . .
END;

15
RAISE_APPLICATION_ERROR
Procedure
Syntax
raise_application_error (error_number,
message[, {TRUE | FALSE}]);

• A procedure that lets you issue user-


defined error messages from stored
subprograms
• Called only from an executing stored
subprogram

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

You might also like