Check OTHERS exception : Handle Exception « PL SQL Programming « Oracle PL/SQL Tutorial






SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE),
  3    First_Name         VARCHAR2(10 BYTE),
  4    Last_Name          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /

Table created.

SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2               values ('01','Jason',    'Martin',  to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto',  'Programmer')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
  3  /

1 row created.

SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /


ID   FIRST_NAME LAST_NAME  START_DAT END_DATE      SALARY CITY       DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01   Jason      Martin     25-JUL-96 25-JUL-06    1234.56 Toronto    Programmer
02   Alison     Mathews    21-MAR-76 21-FEB-86    6661.78 Vancouver  Tester

SQL>
SQL>
SQL>
SQL> DECLARE
  2     v_count NUMBER(10) := 0;
  3
  4  BEGIN
  5
  6     SELECT count(1)
  7     INTO v_count
  8     FROM employee
  9     WHERE id = 5;
 10
 11     IF v_count = 0
 12     THEN
 13        INSERT INTO employee (id, first_name, last_name)
 14        VALUES (5, 'Randy', 'Stauf');
 15     END IF;
 16     ROLLBACK;
 17  EXCEPTION
 18     WHEN OTHERS
 19     THEN
 20        DBMS_OUTPUT.PUT_LINE(SQLERRM);
 21  END;
 22  /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /

Table dropped.








24.15.Handle Exception
24.15.1.Code with No Exception Handler
24.15.2.Code with Conditional Control to Avoid an Exception
24.15.3.Code with Explicit Handler for Predefined Exception
24.15.4.Handling an Unnamed Exception
24.15.5.Handling a custom exception
24.15.6.An example showing continuing program execution after handling exception
24.15.7.The OTHERS Exception Handler
24.15.8.Assigning a Name to Predefined Exception Code
24.15.9.Using SQLCODE for error code and SQLERRM for error message
24.15.10.Catch custom exception
24.15.11.Handling exceptions without halting the program
24.15.12.Select into statement with exception catch statement
24.15.13.Check OTHERS exception
24.15.14.Error message code and text
24.15.15.Using PRAGMA EXCEPTION_INIT
24.15.16.NO data found
24.15.17.TOO_MANY_ROWS Exception
24.15.18.Use a nested block to catch exceptions from singleton SELECT.