WHENEVER
Example
Section titled “Example”DECLARE DivideByZero EXCEPTION;BEGIN WHENEVER SQLERROR RAISE DivideByZero; EXECUTE IMMEDIATE 'SELECT 100/0 FROM dual';EXCEPTION WHEN DivideByZero THEN DBMS_OUTPUT.PUT_LINE('Error: Attempted division by zero.');END;Output
Section titled “Output”Error: Attempted division by zero.Explanation
Section titled “Explanation”In the provided SQL block for Oracle, an exception DivideByZero is declared. The WHENEVER SQLERROR command sets the action Oracle Database should take if it encounters a SQL command with an error.
In this instance, it executes the SQL command SELECT 100/0 FROM dual (which would result in a divide by zero error). The exception DivideByZero is raised and the string ‘Error: Attempted division by zero.’ is printed.