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.

Similar Reads

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....

1. 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....

2. Internally Defined Exception

It is type of exception that are predefined by the PL/SQL runtime environment to handle common error conditions which occur during program execution. It includes exception such as NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, DUP_VAL_ON_INDEX, STORAGE_ERROR etc....

3. Current Exception

It is a type of exception that is currently being handled in the PL/SQL block. It is useful in nested blocks where an exception might be raised in an inner block and you want to reference that specific exception in an outer block. SQLERRM function is used to refer current exception....

Examples of PL/SQL Raise Exception

The below example is demonstrating the use of user-defined exceptions in a real-world scenario. It illustrates how to create a table, a PL/SQL procedure, and handle exceptions related to a specific business rule (salary limit)....

Conclusion

Pl/SQL is a Procedural Language that is used to write program blocks, procedures, functions, cursors, triggers for databases. It provides a Raise exception feature that is used to handle errors in a structured and efficient manner. You can use predefined exceptions, custom exceptions, or referring to the current exception to ensure the code handles unexpected situations....

Contact Us