Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PL/SQL RAISE Exceptions
Next article icon

Exception Propagation in PL/SQL

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
PL/SQL RAISE Exceptions
author
jagan716
Improve
Article Tags :
  • Databases
  • PL/SQL

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences