Error Handling Call : Exception Handle « PL SQL « Oracle PL / SQL






Error Handling Call

    
SQL>
SQL>
SQL> CREATE TABLE emp (
  2     empID INT NOT NULL PRIMARY KEY,
  3     Name      VARCHAR(50) NOT NULL);

Table created.

SQL> INSERT INTO emp (empID,Name) VALUES (1,'Tom');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (2,'Jack');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (3,'Mary');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (4,'Bill');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (5,'Cat');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (6,'Victor');

1 row created.

SQL> CREATE OR REPLACE PROCEDURE ErrorTest(i_StudID IN INT,i_StudName IN VARCHAR)
  2  AS
  3     UnluckyNumber EXCEPTION;
  4  BEGIN
  5     IF i_StudID = 13 THEN
  6        RAISE UnluckyNumber;
  7     END IF;
  8     INSERT INTO emp VALUES (i_StudID, i_StudName);
  9  EXCEPTION
 10     WHEN DUP_VAL_ON_INDEX THEN
 11        dbms_output.put_line('An emp already exists with ID ' || i_StudID);
 12     WHEN UnluckyNumber THEN
 13        dbms_output.put_line('Can''t insert an emp with an unlucky ID');
 14  END;
 15  /

Procedure created.

SQL> CALL ErrorTest(10, 'Jason Fields');

Call completed.

SQL> CALL ErrorTest(13, 'Charles Ives');
Can't insert an emp with an unlucky ID

Call completed.

SQL>
SQL>
SQL> drop table emp;

Table dropped.

SQL>
SQL>

   
    
    
    
  








Related examples in the same category

1.Check exception type
2.Deal with multiple exception branches
3.when other exceptions then
4.Handle update exception
5.declaration exception
6.handle exception of duplicate value on index
7.when other then not user-defined exception
8.Using PRAGMA EXCEPTION_INIT
9.Different Values of SQLCODE and SQLERRM
10.The OTHERS Exception Handler
11.Error-handling features of PL/SQL: log exception
12.PLS-483 error: Duplicate Handlers
13.NO_DATA_FOUND exception.
14.The scope of exceptions.
15.Sub block in exception section
16.Catch all exceptions
17.Catch user-defined exception
18.Check zero divide exception
19.Combines declaring an EXCEPTION variable
20.Insert error message to a table in exception handler
21.Mapping a user-defined error code to an EXCEPTION variable
22.This script demonstrates the EXCEPTION_INIT pragma.
23.Raise exception in if statement