Open In App

Exception Propagation in PL/SQL

Last Updated : 15 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In PL/SQL (Procedural Language/Structured Query Language), efficient error management is crucial for ensuring smooth execution and robustness in database applications. One of the most important features in PL/SQL's exception handling mechanism is exception propagation.

In this article, we will explain the concept of exception propagation in PL/SQL, explaining its significance, how it works, and providing examples to demonstrate how exceptions are managed at different levels.

What is Exception Propagation in PL/SQL?

Exception propagation in PL/SQL refers to the process where an unhandled exception in a PL/SQL block is passed automatically to its enclosing block or the calling environment. Suppose an exception occurs in a block but no handler is present for that exception.

In that case, it propagates to the next outer block, the process will be repeated until the exception is either handled by an appropriate handler or reaches the host environment. If no handler is found, an error message is returned to the user or the calling program.

Syntax:

DECLARE
-- Declarations
BEGIN
-- Executable statements

EXCEPTION
-- Exception-handling section
WHEN <exception_name> THEN
-- Code to handle the exception

WHEN OTHERS THEN
-- Code to handle any other exceptions
END;

Key points for Exception Propagation:

  • If the block raise an exception and there is no handler for it, an exception propagates to enclosing the block.
  • Exception propagation continues through nested blocks until the exception is caught or it reaches the outermost block.
  • If no block handles the exception, an error message is returned to the user or calling program.
  • Propagation is automatically managed by the PL/SQL, and no special directives are required.

Example 1: Exception Propagation with the Predefined Exception (ZERO_DIVIDE)

Predefined exceptions in PL/SQL are built-in exceptions raised by the PL/SQL runtime when specific Oracle error conditions occur. In this example, we will illustrate how a predefined exception, ZERO_DIVIDE, is propagated through nested blocks.

Query:

DECLARE
-- Outer block variable
v_result NUMBER;
BEGIN
-- Outer block
BEGIN
-- Inner block: Raise a division by zero exception
SELECT 10 / 0 INTO v_result FROM dual;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Inner block: No data found.');
-- No handler for ZERO_DIVIDE, so it propagates to the outer block
END;

-- Outer block exception handling
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Outer block: Division by zero error caught.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Outer block: An unexpected error occurred.');
END;
/

Output

Outer block: Division by zero error caught.

Explanation:

  • In the inner block, division by zero raises the ZERO_DIVIDE exception, which is not handled in the inner block.
  • It propagates to the outer block, where ZERO_DIVIDE handler catches it.
  • The message "Outer block: Division by zero error caught" is printed.

Example 2: Propagating a User-defined Exception

The user-defined exception in the PL/SQL is the custom exception that is explicitly declared and raised by developer to the handle specific error conditions that are not covered by the predefined exceptions.

User-defined exceptions allow the developers to define and handle their own error scenarios based on the business logic or the custom requirements.

Query:

DECLARE
-- Define a user-defined exception
e_custom_exception EXCEPTION;
v_result NUMBER;
BEGIN
-- Outer block
BEGIN
-- Inner block: Raise a user-defined exception
RAISE e_custom_exception;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Inner block: No data found.');
-- No handler for e_custom_exception, so it propagates to the outer block
END;

-- Outer block exception handling
EXCEPTION
WHEN e_custom_exception THEN
DBMS_OUTPUT.PUT_LINE('Outer block: Custom exception caught.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Outer block: An unexpected error occurred.');
END;
/

Output

Outer block: Custom exception caught.

Explanation:

  • The inner block raise the user-defined exception (e_custom_exception) , but there is no handler for it in inner block.
  • The exception is to propagate the outer block, where it is the caught by e_custom_exception handler.
  • The message "Outer block: Custom exception caught." is printed.

Example 3: Propagating Exceptions with Multiple Levels of Nesting

This example demonstrates how exceptions propagate through multiple nested blocks. We use a ZERO_DIVIDE exception that is re-raised as a user-defined exception to propagate it further.

Query:

DECLARE
-- Define a user-defined exception
e_custom_exception EXCEPTION;
v_result NUMBER;
BEGIN
-- Outer block
BEGIN
-- Middle block
BEGIN
-- Inner block: Raise a division by zero exception
SELECT 1 / 0 INTO v_result FROM dual;
EXCEPTION
WHEN ZERO_DIVIDE THEN
-- Re-raise a custom exception to propagate it further
RAISE e_custom_exception;
END;
EXCEPTION
-- Handle the custom exception in the middle block
WHEN e_custom_exception THEN
DBMS_OUTPUT.PUT_LINE('Middle block: Custom exception caught.');
END;
EXCEPTION
-- Handle any other exceptions in the outer block
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Outer block: An unexpected error occurred.');
END;
/

Output

Middle block: Custom exception caught.

Explanation:

  • The inner block raises the ZERO_DIVIDE exception, which is caught and re-raised as the custom exception (e_custom_exception).
  • The middle block catches this propagated custom exception and it is handled by printing a message "Middle block: Custom exception caught."
  • Since the exception is caught in middle block, it does not propagate further to outer block.

Conclusion

In conclusion, exception propagation in the PL/SQL is powerful mechanism that is enhance robustness and the reliability of the database applications. By allowing exceptions to propagate through nested blocks, developers can design flexible error-handling strategies.

This prevents errors from being ignored, facilitates organized code, and helps capture unexpected runtime conditions. Understanding exception propagation is crucial for developing resilient PL/SQL programs that can gracefully handle errors, ultimately leading to more reliable and maintainable database applications.

FAQs

How does PL/SQL handle exceptions that are not caught in the current block?

If an exception is raised and not handled in current block, it is automatically propagate to nearest enclosing block. If no block is handled it, the exception is continue to the propagate until it is reach the outermost block or the calling environment.

Can a user-defined exception be propagated like as predefined exception?

Yes, both the predefined and the user-defined exception can be propagate through the blocks. If the user-defined exception is not caught in block where it is raised, it will propagate in same way as the predefined exception.

What happens if an exception is not handled anywhere in the program?

If an exception is not handled in the any block, it is propagate all way to calling environment, which is could be the client application or the database user interface. At that point, the error message is returned to user.

How can I re-raise an exception after catching it?

We can re-raise an exception with the help of RAISE keyword. This is useful for the when we want to handle the exception partially in one block and it propagate it further for the additional handling in the another block.

Can exception propagate through nested PL/SQL blocks?

Yes, exception can be propagate through the multiple layers of the nested PL/SQL blocks. If an exception is raised in the inner block and not handled, it will propagate to next outer block and continuing until it is either caught or returned to calling environment.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg