Give a Name to an Unnamed Error
DECLARE
e_bad_date_format EXCEPTION;
PRAGMA EXCEPTION_INIT (e_bad_date_format, -1830);
BEGIN
DBMS_OUTPUT.put_line (TO_DATE ('2010 10 10 44:55:66', 'YYYSS'));
EXCEPTION
WHEN e_bad_date_format
THEN
DBMS_OUTPUT.put_line ('Bad date format');
END;
Bad date format
Pragma Expects a Negative Integer
DECLARE
my_exception EXCEPTION;
PRAGMA EXCEPTION_INIT (my_exception, 1830);
BEGIN
RAISE my_exception;
END;
ORA-06550: line 2, column 4: PLS-00701: illegal ORACLE error number 1830 for PRAGMA EXCEPTION_INITMore Details: https://docs.oracle.com/error-help/db/ora-06550
Special Case: Can't EXCEPTION_INIT -1403
DECLARE
my_exception EXCEPTION;
PRAGMA EXCEPTION_INIT (my_exception, -1403);
BEGIN
RAISE my_exception;
END;
ORA-06550: line 2, column 4: PLS-00701: illegal ORACLE error number -1403 for PRAGMA EXCEPTION_INITMore Details: https://docs.oracle.com/error-help/db/ora-06550
This One Works
DECLARE
my_exception EXCEPTION;
PRAGMA EXCEPTION_INIT (my_exception, 100);
BEGIN
RAISE my_exception;
END;
ORA-01403: no data foundMore Details: https://docs.oracle.com/error-help/db/ora-06550
Distinguish Between Different Application-Specific Errors
DECLARE
e_bad_data EXCEPTION;
e_bal_too_low EXCEPTION;
PRAGMA EXCEPTION_INIT (e_bal_too_low, -20100);
e_account_closed EXCEPTION;
en_account_closed PLS_INTEGER := -20200;
PRAGMA EXCEPTION_INIT (e_account_closed, -20200);
BEGIN
BEGIN
RAISE e_bad_data;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLCODE);
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack);
END;
BEGIN
RAISE e_bal_too_low;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLCODE);
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack);
END;
BEGIN
RAISE e_account_closed;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLCODE);
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack);
END;
/* Now with RAISE_APPLICATION_ERROR */
BEGIN
RAISE_APPLICATION_ERROR (en_account_closed, 'Account has been closed.');
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLCODE);
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack);
END;
END;
1
ORA-06510: PL/SQL: unhandled user-defined exception
-20100
ORA-20100:
-20200
ORA-20200:
-20200
ORA-20200: Account has been closed.
CREATE TABLE employees AS SELECT * FROM hr.employees
Table created.
The Bad Way: Hard-Coding the Error Code in Exception Section
DECLARE
TYPE namelist_t IS TABLE OF VARCHAR2 (1000);
enames_with_errors namelist_t
:= namelist_t ('ABC', RPAD ('BIGBIGGERBIGGEST', 1000, 'ABC'), 'DEF');
BEGIN
FORALL indx IN 1 .. enames_with_errors.COUNT SAVE EXCEPTIONS
UPDATE employees
SET first_name = enames_with_errors (indx);
EXCEPTION
WHEN OTHERS
THEN
IF SQLCODE = -24381
THEN
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack);
DBMS_OUTPUT.put_line (
'Number of failed statements = ' || SQL%BULK_EXCEPTIONS.COUNT);
ELSE
RAISE;
END IF;
END;
ORA-24381: error(s) in array DML
Number of failed statements = 1
A Better Way to Go: Declare Exception
DECLARE
failure_in_forall EXCEPTION;
PRAGMA EXCEPTION_INIT (failure_in_forall, -24381);
TYPE namelist_t IS TABLE OF VARCHAR2 (1000);
enames_with_errors namelist_t
:= namelist_t ('ABC', RPAD ('BIGBIGGERBIGGEST', 1000, 'ABC'), 'DEF');
BEGIN
FORALL indx IN 1 .. enames_with_errors.COUNT SAVE EXCEPTIONS
UPDATE employees
SET first_name = enames_with_errors (indx);
EXCEPTION
WHEN failure_in_forall
THEN
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack);
DBMS_OUTPUT.put_line (
'Number of failed statements = ' || SQL%BULK_EXCEPTIONS.COUNT);
END;
ORA-24381: error(s) in array DML
Number of failed statements = 1
The Best Approach: Declare Exception in Package Specification
CREATE OR REPLACE PACKAGE app_errs_pkg
IS
failure_in_forall EXCEPTION;
PRAGMA EXCEPTION_INIT (failure_in_forall, -24381);
END;
Package created.
No Need for Local Declaration of Exception
DECLARE
TYPE namelist_t IS TABLE OF VARCHAR2 (1000);
enames_with_errors namelist_t
:= namelist_t ('ABC', RPAD ('BIGBIGGERBIGGEST', 1000, 'ABC'), 'DEF');
BEGIN
FORALL indx IN 1 .. enames_with_errors.COUNT SAVE EXCEPTIONS
UPDATE employees
SET first_name = enames_with_errors (indx);
EXCEPTION
WHEN app_errs_pkg.failure_in_forall
THEN
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack);
DBMS_OUTPUT.put_line (
'Number of failed statements = ' || SQL%BULK_EXCEPTIONS.COUNT);
END;
ORA-24381: error(s) in array DML
Number of failed statements = 1