How to Raise Exceptions in PL/SQL?
PL/SQL Raise Exception is a feature that helps us to handle errors in a structured and efficient manner. Exceptions in PL/SQL are used to manage unexpected situations and ensure the integrity of the database. There are three ways to Raise an exception in PL/SQL.
- User-Defined Exception.
- Internally Defined Exception.
- Current Exception.
Syntax:
DECLARE
custom_exception EXCEPTION;
BEGIN
-- Some condition that may warrant raising a user-defined exception
IF some_condition THEN
-- Raise a user-defined exception
RAISE custom_exception;
END IF;
-- Rest of the code...
EXCEPTION
WHEN custom_exception THEN
-- Handle the user-defined exception
DBMS_OUTPUT.PUT_LINE('Custom exception handled!');
END;
Explanation:
- if some_condition is true then the RAISE custom_exception statement will be executed and make happen the user-defined exception custom_exception to be raised.
- The WHEN custom_exception THEN block in the EXCEPTION section will catch and handle this exception.
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. It is a block-structured language that combines SQL with the procedural features of programming languages. In this article, we will learn about RAISE Exception in PL/SQL with its syntax, types, and examples.
Contact Us