What happens if an exception is raised in the program and that exception is not handled by an exception section in either the current or enclosing PL SQL blocks?

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 

The best on site "Oracle training classes" are just a phone call away! You can get personalized Oracle training by Donald Burleson, right at your shop!


 
 

 

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.

What happens if an exception is raised and there are no handler for it in the current block?

Handlers in the current block cannot catch the raised exception because an exception raised in a declaration propagates immediately to the enclosing block.

In which section of a PL SQL block is a user

Exceptions can be declared only in the declarative part of a PL/SQL block, subprogram, or package. You declare an exception by introducing its name, followed by the keyword EXCEPTION .

How would you handle exceptions and still continue to process a PL SQL procedure?

By putting a BEGIN-END block with an exception handler inside of a loop, you can continue executing the loop if some loop iterations raise exceptions. You can still handle an exception for a statement, then continue with the next statement. Place the statement in its own subblock with its own exception handlers.

Which section of the PL SQL block handles errors and abnormal conditions declaration exception executable begin?

Such errors must be trapped and handled in the EXCEPTION section of the PL/SQL block. The exception handlers can suppress the abnormal termination with an alternative and secured action. Exception handling is one of the important steps of database programming.

Bài Viết Liên Quan

Chủ đề