Now that you know how to cause a system generated exception, you can learn how to handle it. In this example, a handler that is specifically designed to address divide-by-zero exceptions will simply output a message.
DECLARE x NUMBER; BEGIN dbms_output.put_line('Starting...'); x := 1 / 0; dbms_output.put_line('...Ending'); EXCEPTION WHEN ZERO_DIVIDE THEN DBMS_OUTPUT.PUT_LINE('Divide by zero error.'); END;
Each PL/SQL block can optionally include an EXCEPTION section where specific exceptions and errors are captured and dealt with programmatically. Specific error types can be handled by indicating the error in a specific WHEN clause.