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.
DECLARE x NUMBER; BEGIN x := 1 / 0; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Some exception caused this to display.'); END;
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.
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;
The divide-by-zero error code and the first one hundred characters of the error message are displayed when this code is executed.