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

Lesson 8

This document discusses error handling in PL/SQL. It covers identifying common exceptions, describing the three basic types of exceptions, and writing exception handling routines. It provides examples of trapping predefined Oracle server errors, non-predefined errors, and user-defined exceptions using exception handlers. It also discusses functions like WHEN OTHERS, SQLCODE, and SQLERRM that can be used for trapping exceptions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lesson 8

This document discusses error handling in PL/SQL. It covers identifying common exceptions, describing the three basic types of exceptions, and writing exception handling routines. It provides examples of trapping predefined Oracle server errors, non-predefined errors, and user-defined exceptions using exception handlers. It also discusses functions like WHEN OTHERS, SQLCODE, and SQLERRM that can be used for trapping exceptions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lesson 8

Error Handling
Topic Coved

• DBMS and RDBMS


• Installation of Oracle Database 21c XE and Oracle SQL Developer
• Oracle SQL (Querying data, Sorting data, Filtering data)
• Grouping and Joining tables and Sub queries
• Views, Indexes and Sequences
• Regular Expressions
• PL/SQL Blokes
• Control Structure, Iterative processing with loops, Select Into in PL/SQL
• Cursors, Records and Stored procedures and Functions
• Exception handlers
• Program Units
• Database Triggers
• PL/SQL Collections
• Dynamic SQL

2-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Objectives

• Identify common exceptions.


• Describe the three basic types of exceptions.
• Write exception handling routines.

3-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Overview

• 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.

4-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


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
5-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Trapping Exceptions: Guidelines

• WHEN OTHERS is the last clause.


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

6-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


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

7-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Predefined Exception: Example

PROCEDURE
PROCEDURE elim_inventory
elim_inventory
(v_product_id
(v_product_id IN
IN s_product.id%TYPE)
s_product.id%TYPE) IS
IS
v_id
v_id s_product.id%TYPE;
s_product.id%TYPE;
BEGIN
BEGIN
SELECT
SELECT id
id
INTO
INTO v_id
v_id
FROM
FROM s_product
s_product
WHERE
WHERE id
id == v_product_id;
v_product_id;
DELETE
DELETE FROM
FROM s_inventory
s_inventory
WHERE
WHERE product_id
product_id == v_product_id;
v_product_id;
COMMIT;
COMMIT;
...
...

8-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Predefined Exception: Example

...
...
EXCEPTION
EXCEPTION
WHEN
WHEN NO_DATA_FOUND
NO_DATA_FOUND THEN
THEN
ROLLBACK;
ROLLBACK;
TEXT_IO.PUT_LINE(TO_CHAR(v_product_id))||
TEXT_IO.PUT_LINE(TO_CHAR(v_product_id))||
’’ is
is invalid.’);
invalid.’);
WHEN
WHEN TOO_MANY_ROWS
TOO_MANY_ROWS THEN
THEN
ROLLBACK;
ROLLBACK;
TEXT_IO.PUT_LINE
TEXT_IO.PUT_LINE (’Data
(’Data corruption
corruption in
in S_PRODUCT.’);
S_PRODUCT.’);
WHEN
WHEN OTHERS
OTHERS THEN
THEN
ROLLBACK;
ROLLBACK;
TEXT_IO.PUT_LINE
TEXT_IO.PUT_LINE (’Other
(’Other error
error occurred.’);
occurred.’);
END
END elim_inventory;
elim_inventory;

9-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Trapping Non-Predefined Oracle
Server Errors

Declare Associate Reference

Declarative Section Exception-handling


Section

• Name the • Code the pragma • Handle


exception EXCEPTION_INIT the raised
exception

10-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Non-Predefined Error: Example
Trap for Oracle Server error number -2292 an
integrity constraint violation.
[DECLARE]
[DECLARE]
e_products_remaining 1
e_products_remaining EXCEPTION;
EXCEPTION;
PRAGMA
PRAGMA EXCEPTION_INIT
EXCEPTION_INIT ((
e_products_remaining, 2
e_products_remaining, -2292);
-2292);
.. .. ..
BEGIN
BEGIN
.. .. ..
EXCEPTION
EXCEPTION 3
WHEN
WHEN e_products_remaining
e_products_remaining THEN
THEN
TEXT_IO.PUT_LINE
TEXT_IO.PUT_LINE ('Referential
('Referential integrity
integrity
constraint
constraint violated.');
violated.');
.. .. ..
END;
END;
11-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Trapping User-Defined Exceptions

Declare Raise Reference

Declarative Executable Exception-handling


Section Section Section

• Name the • Explicitly raise the • Handle the


exception exception by using raised
the RAISE exception
statement

12-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


User-Defined Exception: Example
If there is product in stock, halt processing, and
print a message to the user.
[DECLARE]
[DECLARE]
e_amount_remaining
e_amount_remaining EXCEPTION;
EXCEPTION; 1
.. .. ..
BEGIN
BEGIN
.. .. ..
RAISE
RAISE e_amount_remaining;
e_amount_remaining; 2
.. .. ..
EXCEPTION
EXCEPTION
WHEN 3
WHEN e_amount_remaining
e_amount_remaining THEN
THEN
TEXT_IO.PUT_LINE
TEXT_IO.PUT_LINE ('There
('There is
is still
still an
an amount
amount
in
in stock.');
stock.');
.. .. ..
END;
END;
13-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Functions for Trapping Exceptions

• WHEN OTHERS exception handler


– Traps all exceptions not yet handled.
– Is the last handler.
• SQLCODE
– Returns the numeric value for the error code.
• SQLERRM
– Returns the message associated with the error
number.

14-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Functions for Trapping Exceptions: Example
Store the error code and error message for any
unanticipated exception.
.. .. ..
v_error_code
v_error_code NUMBER;
NUMBER;
v_error_message
v_error_message VARCHAR2(255);
VARCHAR2(255);
BEGIN
BEGIN
.. .. ..
EXCEPTION
EXCEPTION
.. .. ..
WHEN
WHEN OTHERS
OTHERS THEN
THEN
ROLLBACK;
ROLLBACK;
v_error_code
v_error_code :=:= SQLCODE
SQLCODE;;
v_error_message
v_error_message := := SQLERRM
SQLERRM;;
TEXT_IO.PUT_LINE
TEXT_IO.PUT_LINE (TO_CHAR(v_error_code)||
(TO_CHAR(v_error_code)||
':
': '||
'|| v_error_message);
v_error_message);
END;
END;

15-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Calling Environments

SQL*Plus Displays error number and message


to screen.
Procedure Displays error number and message
Builder to screen.
Developer/2000 Accesses error number and message
Forms in a trigger.
Precompiler Accesses exception number through
Application SQLCA.
An Enclosing Traps exception in exception
PL/SQL Block handling routine of enclosing block.

16-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Propagating Exceptions
DECLARE
...
;exceptione_no_rows
;exceptione_integrity
( PRAGMA EXCEPTION_INIT
;)e_integrity, -2292
BEGIN
FOR c_record IN emp_cursor LOOP
Sub-blocks can handle BEGIN
SELECT . . .
an exception or pass UPDATE . . .
IF SQL%NOTFOUND THEN
the exception to the RAISE e_no_rows;
END IF;
enclosing block. EXCEPTION
WHEN e_integrity THEN . . .
WHEN e_no_rows THEN . . .
END;
;END LOOP
EXCEPTION
. . . WHEN NO_DATA_FOUND THEN
. . . WHEN TOO_MANY_ROWS THEN
;END
17-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Summary

• Program for exceptions that may arise during the


execution of the PL/SQL block.
• Exception types:
– Predefined Oracle Server error
– Non-Predefined Oracle Server error
– User-defined
• Handling exceptions:
– Trapping
– Propagating
18-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright


Practice Overview

• Handling named exceptions


• Creating and invoking user-defined exceptions

19-25

.Oracle Corporation, 1996. All rights reserved ‫ س‬Copyright

You might also like