Infolinks

Friday, 10 August 2012

Oracle/PLSQL: Named System Exceptions


Oracle/PLSQL: Named System Exceptions


What is a named system exception?

Named system exceptions are exceptions that have been given names by PL/SQL. They are named in the STANDARD package in PL/SQL and do not need to be defined by the programmer.
Oracle has a standard set of exceptions already named as follows:
Oracle Exception NameOracle ErrorExplanation
DUP_VAL_ON_INDEXORA-00001You tried to execute an INSERT or UPDATE statement that has created a duplicate value in a field restricted by a unique index.
TIMEOUT_ON_RESOURCEORA-00051You were waiting for a resource and you timed out.
TRANSACTION_BACKED_OUTORA-00061The remote portion of a transaction has rolled back.
INVALID_CURSORORA-01001You tried to reference a cursor that does not yet exist. This may have happened because you've executed a FETCH cursor or CLOSE cursor before OPENing the cursor.
NOT_LOGGED_ONORA-01012You tried to execute a call to Oracle before logging in.
LOGIN_DENIEDORA-01017You tried to log into Oracle with an invalid username/password combination.
NO_DATA_FOUNDORA-01403You tried one of the following:
  1. You executed a SELECT INTO statement and no rows were returned.
  2. You referenced an uninitialized row in a table.
  3. You read past the end of file with the UTL_FILE package.
TOO_MANY_ROWSORA-01422You tried to execute a SELECT INTO statement and more than one row was returned.
ZERO_DIVIDEORA-01476You tried to divide a number by zero.
INVALID_NUMBERORA-01722You tried to execute an SQL statement that tried to convert a string to a number, but it was unsuccessful.
STORAGE_ERRORORA-06500You ran out of memory or memory was corrupted.
PROGRAM_ERRORORA-06501This is a generic "Contact Oracle support" message because an internal problem was encountered.
VALUE_ERRORORA-06502You tried to perform an operation and there was a error on a conversion, truncation, or invalid constraining of numeric or character data.
CURSOR_ALREADY_OPENORA-06511You tried to open a cursor that is already open.
The syntax for the Named System Exception in a procedure is:
CREATE [OR REPLACE] PROCEDURE procedure_name
    [ (parameter [,parameter]) ]
IS
    [declaration_section]
BEGIN
    executable_section
EXCEPTION
    WHEN exception_name1 THEN
        [statements]
    WHEN exception_name2 THEN
        [statements]
    WHEN exception_name_n THEN
        [statements]
    WHEN OTHERS THEN
        [statements]
END [procedure_name];

The syntax for the Named System Exception in a function is:
CREATE [OR REPLACE] FUNCTION function_name
    [ (parameter [,parameter]) ]
    RETURN return_datatype
IS | AS
    [declaration_section]
BEGIN
    executable_section
EXCEPTION
    WHEN exception_name1 THEN
        [statements]
    WHEN exception_name2 THEN
        [statements]
    WHEN exception_name_n THEN
        [statements]
    WHEN OTHERS THEN
        [statements]
END [function_name];

Here is an example of a procedure that uses a Named System Exception:
CREATE OR REPLACE PROCEDURE add_new_supplier
    (supplier_id_in IN NUMBER, supplier_name_in IN VARCHAR2)
IS
BEGIN
    INSERT INTO suppliers (supplier_id, supplier_name )
    VALUES ( supplier_id_in, supplier_name_in );
EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
        raise_application_error (-20001,'You have tried to insert a duplicate supplier_id.');
    WHEN OTHERS THEN
        raise_application_error (-20002,'An error has occurred inserting a supplier.');
END;
In this example, we are trapping the Named System Exception called DUP_VAL_ON_INDEX. We are also using the WHEN OTHERS clause to trap all remaining exceptions.

No comments:

Post a Comment