OTHERS Exception Handler

Contact Us or call 1-877-932-8228
OTHERS Exception Handler

OTHERS Exception Handler

The exception handlers specified up to this point were targeted for specific exceptions. An optional OTHERS exception handler can be specified to handle all exceptions not named specifically. The WHEN OTHERS clause must be the last handler specified in a block.

Code Sample:

Exception-Clause/Demos/exception_when_others.sql
DECLARE
  x NUMBER;
BEGIN
  x := 1 / 0;
EXCEPTION
  WHEN OTHERS THEN 
  
    DBMS_OUTPUT.PUT_LINE('Some exception caused this to display.');
END;

Code Explanation

All errors not specifically handled can be caught by using the WHEN OTHERS clause.

If you need to use a WHEN OTHERS exception handler, you might be interested in identifying what exception occurred and is being handled. The SQLCODE and SQLERM functions can be used to return the error code and message text.

Code Sample:

Exception-Clause/Demos/error_code_and_message.sql
DECLARE
   x NUMBER;
BEGIN
   x:= 1 / 0;
EXCEPTION
   WHEN OTHERS THEN
      dbms_output.put_line('Err number:   ' ||
                           SQLCODE);
      dbms_output.put_line('Error Message:' ||
                           SUBSTR(SQLERRM, 1, 100));
END;

Code Explanation

The divide-by-zero error code and the first one hundred characters of the error message are displayed when this code is executed.

Next