This is an excerpt from the book Advanced PL/SQL: The Definitive Reference by Boobal Ganesan.
An exception can be re-raised either to pass the control of the exception to the enclosing block or to log the error into a table and then raise it to an application or to a user. To re-raise an exception, the command RAISE has to be placed in the exception section after the logging of the information is done.
%Note: The exception name can be optionally placed after the RAISE command.
In the below example, the exception ZERO_DIVIDE is logged into a table right before it is re-raised to the user or to the application.
1. SET SERVEROUTPUT ON SIZE 200000
2. DECLARE
3. l_n_var1 NUMBER;
4. BEGIN
5. L_n_var1:= 1/0;
6. EXCEPTION
7. WHEN zero_divide THEN
8. INSERT INTO log_table VALUES (log_seq.nextval,SQLCODE ||' '||sqlerrm);
9. raise;
10.END;
11./
Script Explanation:
Line No. | Description |
1 | This environment variable opens up an output buffer of size limit of 200000. |
2 | Start of the declaration section of the block. |
3 | A local variable l_n_var1 of the number data type is declared. |
4 | Start of the execution section of the block. |
5 | The integer 1 is divided by the integer 0 and is assigned to the variable l_n_var1. |
6 | Start of the exception section of the block. |
7 | A when clause handling the predefined exception zero_divide is defined. |
8 | The error code (SQLCODE) and the error message (SQLERRM) values are logged into a table. |
9 | The exception raise is re-raised using the RAISE statement. |
10,11 | End of the exception section of the block. |
Need to learn to program with PL/SQL? For complete notes on programming in PL/SQL, we recommend the book Advanced PL/SQL: The Definitive Reference by Boobal Ganesan. This is a complete book on PL/SQL with everything you need to know to write efficient and complex PL/SQL code. |
Oracle Training from Don Burleson | ||||||
Burleson is the American Team
Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals. Feel free to ask questions on our Oracle forum. Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications. Errata? Oracle technology is changing and we strive to update our BC Oracle support information. If you find an error or have a suggestion for improving our content, we would appreciate your feedback. Just e-mail: and include the URL for the page. Copyright © 1996 - 2020 All rights reserved by Burleson Oracle ® is the registered trademark of Oracle Corporation. |