Syntax
We will take a look at the syntax for the WHEN OTHERS clause in both procedures and functions.
Syntax for Procedures
The syntax for the WHEN OTHERS clause in a procedure is:
Syntax for Functions
The syntax for the WHEN OTHERS clause in a function is:
Example
Here is an example of a procedure that uses a WHEN OTHERS clause:
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.
Frequently Asked Questions
Question: Is there any way to get the ORA error number (and/or description) for the errors that will fall into OTHERS?
Something like:
Answer: Yes, you can use SQLCODE function to retrieve the error number and SQLERRM function to retrieve the error message.
For example, you could raise the error as follows:
Or you could log the error to a table as follows: