Académique Documents
Professionnel Documents
Culture Documents
Syntax
We will take a look at the syntax for Named Programmer-Defined Exceptions in both procedures
and functions.
exception_name EXCEPTION;
BEGIN
executable_section
RAISE exception_name;
EXCEPTION
WHEN exception_name THEN
[statements]
END [procedure_name];
Syntax for Functions
IS | AS
[declaration_section]
exception_name EXCEPTION;
BEGIN
executable_section
RAISE exception_name;
EXCEPTION
WHEN exception_name THEN
[statements]
END [function_name];
Example
Here is an example of a procedure that uses a Named Programmer-Defined Exception:
BEGIN
IF sales_in = 0 THEN
RAISE no_sales;
ELSE
INSERT INTO orders (order_id, total_sales )
VALUES ( order_id_in, sales_in );
END IF;
EXCEPTION
WHEN no_sales THEN
raise_application_error (-20001,'You must have sales in order to submit
the order.');
no_sales EXCEPTION;
We've then raised the exception in the executable section of the code:
IF sales_in = 0 THEN
RAISE no_sales;
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:
We are also using the WHEN OTHERS clause to trap all remaining exceptions:
Oracle
Oracle Exception Name Explanation
Error
ORA-
TIMEOUT_ON_RESOURCE You were waiting for a resource and you timed out.
00051
ORA-
TRANSACTION_BACKED_OUT The remote portion of a transaction has rolled back.
00061
You tried to reference a cursor that does not yet exist. This may
ORA-
INVALID_CURSOR have happened because you've executed a FETCH cursor or
01001
CLOSE cursor before OPENing the cursor.
ORA-
NOT_LOGGED_ON You tried to execute a call to Oracle before logging in.
01012
ORA- You tried to execute a SELECT INTO statement and more than
TOO_MANY_ROWS
01422 one row was returned.
ORA-
ZERO_DIVIDE You tried to divide a number by zero.
01476
ORA-
STORAGE_ERROR You ran out of memory or memory was corrupted.
06500
ORA-
CURSOR_ALREADY_OPEN You tried to open a cursor that is already open.
06511
Syntax
We will take a look at the syntax for Named System Exceptions in both procedures and
functions.
BEGIN
executable_section
EXCEPTION
WHEN exception_name1 THEN
[statements]
WHEN exception_name2 THEN
[statements]
END [procedure_name];
BEGIN
executable_section
EXCEPTION
WHEN exception_name1 THEN
[statements]
END [function_name];
Example
Here is an example of a procedure that uses a Named System Exception:
BEGIN
INSERT INTO suppliers (supplier_id, supplier_name )
VALUES ( supplier_id_in, supplier_name_in );
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001,'You have tried to insert a duplicate
supplier_id.');
END;
In this example, we are trapping the Named System Exception called DUP_VAL_ON_INDEX.
We are also using the WHEN OTHERS clause to trap all remaining exceptions.
Oracle / PLSQL: WHEN OTHERS Clause
This Oracle tutorial explains how to use the Oracle WHEN OTHERS clause with syntax and
examples.
Syntax
We will take a look at the syntax for the WHEN OTHERS clause in both procedures and
functions.
BEGIN
executable_section
EXCEPTION
WHEN exception_name1 THEN
[statements]
END [procedure_name];
BEGIN
executable_section
EXCEPTION
WHEN exception_name1 THEN
[statements]
END [function_name];
Example
Here is an example of a procedure that uses a WHEN OTHERS clause:
BEGIN
IF sales_in = 0 THEN
RAISE no_sales;
ELSE
INSERT INTO orders (order_id, total_sales )
VALUES ( order_id_in, sales_in );
END IF;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001,'You have tried to insert a duplicate
order_id.');
END;
In this example, if an exception is encountered that is not a DUP_VAL_ON_INDEX or a
no_sales, it will be trapped by the WHEN OTHERS clause.
Something like:
Answer: Yes, you can use SQLCODE function to retrieve the error number and SQLERRM
function to retrieve the error message.
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered -
'||SQLCODE||' -ERROR- '||SQLERRM);
END;
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 200);
Syntax
The syntax for the SQLCODE function in Oracle/PLSQL is:
SQLCODE
Parameters or Arguments
Example
Since EXCEPTION HANDLING is usually written with the following syntax:
EXCEPTION
WHEN exception_name1 THEN
[statements]
END [procedure_name];
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered -
'||SQLCODE||' -ERROR- '||SQLERRM);
END;
Or you could log the error to a table using the SQLCODE function as follows:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 200);
Syntax
The syntax for the SQLERRM function in Oracle/PLSQL is:
SQLERRM
Parameters or Arguments
Example
Since EXCEPTION HANDLING is usually written with the following syntax:
EXCEPTION
WHEN exception_name1 THEN
[statements]
END [procedure_name];
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered -
'||SQLCODE||' -ERROR- '||SQLERRM);
END;
Or you could log the error to a table using the SQLERRM function as follows:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 200);