转载: PL/SQL RAISE Exceptions By Practical Examples
Summary: in this tutorial, you will learn how to use the PL/SQL RAISE
statement to raise a user-defined exception, internally defined exception, and reraising an exception.
To raise an exception explicitly, you use the RAISE
statement. The RAISE
statement allows you to:
- Raise a user-defined exception.
- Raise an internally defined exception.
- Reraising the current exception.
# Raising a user-defined exception
A user-defined exception is defined by users like you or other developers in the declaration section of a block or subprogram.
# Declaring a user-defined exception
To define a user-defined exception, you use the following syntax:
1 | DECLARE |
Similar to the variable declaration, you declare an exception in the declaration section of a block.
A user-defined exception must have assigned error_code
. To do it, you use the EXCEPTION_INIT
pragma as follows:
1 | PRAGMA EXCEPTION_INIT(exception_name, error_code) |
In this syntax, the error_code
is an integer that ranges from -20,999
to -20,000
. And the message
is a character string with a maximum length of 2,048
bytes.
The entire syntax for declaring a user-defined exception is as follows:
1 | DECLARE |
# Raising a user-defined exception example
The following example illustrates how to declare a user-defined exception and associate it with an error code.
1 | DECLARE |
In this example,
- First, declare a user-defined exception
e_credit_too_high
and associates it with the error number-20001
. - Second, select maximum credit from the
customers
table using theMAX()
function and assign this value to thel_max_credit
variable. - Third, check if the input credit with the maximum credit, if the input credit is greater than the max, then raise the
e_credit_too_high
exception. - Finally, update the customer whose id is entered by the user with the new credit limit.
Here is the output if you enter customer id 100 and credit limit 20000
:
1 | ORA-20001: |
If you want to include a custom message, you can replace the line:
1 | RAISE e_credit_too_high; |
by the following line:
1 | raise_application_error(-20001,'Credit is too high'); |
And execute the code block again, you will receive the following error:
1 | ORA-20001:Creditistoohigh |
# Raising an internally defined exception
Typically, the runtime system raises internally defined exceptions implicitly when they occur. Besides, you can explicitly raise an internally defined exception with the RAISE
statement if the exception has a name:
1 | RAISE exception_name; |
This example shows how to raise an internally defined exception INVALID_NUMBER
:
1 | DECLARE |
If you execute the block and enter the customer id -10, you will get the following error:
1 | ORA-01722:invalidnumber |
# Reraising the current exception
You can re-raise the current exception with the RAISE
statement. Reraising an exception passes it to the enclosing block, which later can be handled further. To reraise an exception, you don’t need to specify the exception name.
1 | DECLARE |
In this example:
- First, get the max credit limit from the
customers
table. - Second, compare the max credit with the user-input credit. If the user-input credit is greater than the max credit, then raise the
e_credit_too_high
exception. - Third, display a message and reraise the exception in the exception-handling section in the inner block.
- Finally, in the outer block, reassign the average credit to the
l_credit
variable and update the customer with the newly adjusted credit.
If you enter the customer id 100 and credit limit 10000, the credit limit of the customer will be updated to the average credit.
1 | SELECT * FROM customers WHERE customer_id=100; |
In this tutorial, you have learned how to use the PL/SQL RAISE
statement to explicitly raise a user-defined exception, internally defined exception, and reraising an exception.