User-Defined Exception
It is a type of exception that is defined by a developer to handle specific error conditions or business rules in their PL/SQL code. we can declare a user-defined exception using the EXCEPTION keyword and we can raise it using the RAISE statement.
Example:
DECLARE
custom_exception EXCEPTION;
BEGIN
IF true THEN
-- If a specific condition is met, raise the user-defined exception
RAISE custom_exception;
END IF;
EXCEPTION
WHEN custom_exception THEN
DBMS_OUTPUT.PUT_LINE('Custom exception handled!');
END;
Output:
Statement processed.
Custom exception handled!
Explanation: In the above query we have declares a custom exception custom_exception
. If a condition (in this case, always true for demonstration purposes) is met then it raises the custom_exception
. The EXCEPTION
block catches and handles the custom_exception
, printing ‘Custom exception handled!‘ using DBMS_OUTPUT
.
PUT_LINE
.
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