Oracle / PLSQL: Named Programmer-Defined Exceptions
This Oracle tutorial explains how to use Named Programmer-Defined Exceptions in Oracle/PLSQL with syntax and examples.
What is a named programmer-defined exception in Oracle?
Sometimes, it is necessary for programmers to name and trap their own exceptions - ones that aren't defined already by PL/SQL. These are called Named Programmer-Defined Exceptions.
Syntax
We will take a look at the syntax for Named Programmer-Defined Exceptions in both procedures and functions.
Syntax for Procedures
The syntax for the Named Programmer-Defined Exception in a procedure is:
Syntax for Functions
The syntax for the Named Programmer-Defined Exception in a function is:
Example
Here is an example of a procedure that uses a Named Programmer-Defined Exception:
In this example, we have declared a Named Programmer-Defined Exception called no_sales in our declaration statement with the following code:
We've then raised the exception in the executable section of the code:
Now if the sales_in variable contains a zero, our code will jump directly to the Named Programmer-Defined Exception called no_sales.
Finally, we tell our procedure what to do when the no_sales exception is encountered by including code in the WHEN clause: