Exception Propagation in PL/SQL
Last Updated : 15 Oct, 2024
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.
Similar Reads
PL/SQL RAISE Exceptions
PL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases. It extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. PL/SQL provides a structured approach
5 min read
Exception Handling in Programming
Exception handling is a critical aspect of programming, enabling developers to manage unexpected or erroneous situations gracefully. In this article, we'll discuss the concept of exception handling, its importance, and best practices for implementing it effectively in various programming languages.
7 min read
Python Print Exception
In Python, exceptions are errors that occur at runtime and can crash your program if not handled. While catching exceptions is important, printing them helps us understand what went wrong and where. In this article, we'll focus on different ways to print exceptions. Using as keywordas keyword lets u
3 min read
Raising Exceptions in Ruby
An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at runtime, that disrupts the normal flow of the programâs instructions. As we know, the code enclosed between begin and end block is totally secured for handling Exceptions and the rescue block tells
4 min read
Polymorphic Exceptions In C++
C++ is a powerful programming language that allows developers to handle errors and exceptional situations using exceptions. In this article, we will explore the concept of polymorphic exceptions in C++. Polymorphic exceptions allow you to create custom exception classes that can be caught at differe
3 min read
SQLite Except Operator
SQLite is a server-less database engine written in C programming language. It is developed by D. Richard Hipp in the year 2000. The main motive for developing SQLite is to escape from complex database engines like MySQL etc. It has become one of the most popular database engines as we use it in Tele
5 min read
Spring MVC - Exception Handling
Prerequisites: Spring MVC When something goes wrong with your application, the server displays an exception page defining the type of exception, the server-generated exception page is not user-friendly. Spring MVC provides exception handling for your web application to make sure you are sending your
6 min read
Exception handling in Julia
Any unexpected condition that occurs during the normal program execution is called an Exception. Exception Handling in Julia is the mechanism to overcome this situation by chalking out an alternative path to continue normal program execution. If exceptions are left unhandled, the program terminates
6 min read
Rethrowing an Exception in C++
Exception handling plays a role in developing robust software in C++. It offers a way to handle errors and unexpected situations. One interesting aspect of exception handling in C++ is the ability to rethrow an exception allowing it to pass up the call stack. Rethrowing an ExceptionRethrowing an exc
5 min read
Decision Making in PL/SQL
PL/SQL (Procedural Language/Structured Query Language) is Oracle's extension to SQL that allows for procedural programming within databases. It features various conditional statements to control the flow of execution based on specific conditions. In this article, We will learn about the various PL/S
5 min read