PL/SQL Tutorials - HANDLING ERRORS AND EXCEPTIONS

PLSQL handles the errors caused at the server level and publishes them using the following functions:

EXCEPTION This datatype is declared in DECLARE section of the PLSQL code enables to call those as and when they are required.

An internal exception is raised implicitly whenever your PL/SQL program violates an Oracle rule or exceeds a system-dependent limit. Every Oracle error has a number, but exceptions must be handled by name. So, PL/SQL predefines some common Oracle errors as exceptions. For example, PL/SQL raises the predefined exception NO_DATA_FOUND if a SELECT INTO statement returns no rows.

To handle other Oracle errors, you can use the OTHERS handler.

The functions SQLCODE and SQLERRM are especially useful in the OTHERS handler because they return the Oracle error code and message text. Alternatively, you can use the pragma EXCEPTION_INIT to associate exception names with Oracle error codes.

Exception: ACCESS_INTO_NULL
Oracle Error: ORA-06530
SQLCODE: -6530

Your program attempts to assign values to the attributes of an uninitialized (atomically null) object.

Exception: CASE_NOT_FOUND
Oracle Error: ORA-06592
SQLCODE: -6592

None of the choices in the WHEN clauses of a CASE statement is selected, and there is no ELSE clause.

Exception: COLLECTION_IS_NULL
Oracle Error: ORA-06531
SQLCODE: -6531

Your program attempts to apply collection methods other than EXISTS to an uninitialized (atomically null) nested table or varray, or the program attempts to assign values to the elements of an uninitialized nested table or varray.

Exception: CURSOR_ALREADY_OPEN
Oracle Error: ORA-06511
SQLCODE: -6511

Your program attempts to open an already open cursor. A cursor must be closed before it can be reopened. A cursor FOR loop automatically opens the cursor to which it refers. So, your program cannot open that cursor inside the loop.

Exception: DUP_VAL_ON_INDEX
Oracle Error: ORA-00001
SQLCODE: -1

Your program attempts to store duplicate values in a database column that is constrained by a unique index.

Exception: INVALID_CURSOR
Oracle Error: ORA-01001
SQLCODE: -1001

Your program attempts an illegal cursor operation such as closing an unopened cursor.

Exception: INVALID_NUMBER
Oracle Error: ORA-01722
SQLCODE: -1722

In a SQL statement, the conversion of a character string into a number fails because the string does not represent a valid number. (In procedural statements, VALUE_ERROR is raised.) This exception is also raised when the LIMIT-clause expression in a bulk FETCH statement does not evaluate to a positive number.

Exception: LOGIN_DENIED
Oracle Error: ORA-01017
SQLCODE: -1017

Your program attempts to log on to Oracle with an invalid username and/or password.

Exception: NO_DATA_FOUND
Oracle Error: ORA-01403
SQLCODE: +100

A SELECT INTO statement returns no rows, or your program references a deleted element in a nested table or an un-initialized element in an index-by table. SQL aggregate functions such as AVG and SUM always return a value or a null. So, a SELECT INTO statement that calls an aggregate function never raises NO_DATA_FOUND. The FETCH statement is expected to return no rows eventually, so when that happens, no exception is raised.

Exception: NOT_LOGGED_ON
Oracle Error: ORA-01012
SQLCODE: -1012

Your program issues a database call without being connected to Oracle.

Exception: PROGRAM_ERROR
Oracle Error: ORA-06501
SQLCODE: -6501

PL/SQL has an internal problem.

Exception: ROWTYPE_MISMATCH
Oracle Error: ORA-06504
SQLCODE: -6504

The host cursor variable and PL/SQL cursor variable involved in an assignment have incompatible return types. For example, when an open host cursor variable is passed to a stored subprogram, the return types of the actual and formal parameters must be compatible.

Exception: SELF_IS_NULL
Oracle Error: ORA-30625
SQLCODE: -30625

Your program attempts to call a MEMBER method on a null instance. That is, the built-in parameter SELF (which is always the first parameter passed to a MEMBER method) is null.

Exception: STORAGE_ERROR
Oracle Error: ORA-06500
SQLCODE: -6500

PL/SQL runs out of memory or memory has been corrupted.

Exception: SUBSCRIPT_BEYOND_COUNT
Oracle Error: ORA-06533
SQLCODE: -6533

Your program references a nested table or varray element using an index number larger than the number of elements in the collection.

Exception: SUBSCRIPT_OUTSIDE_LIMIT
Oracle Error: ORA-06532
SQLCODE: -6532

Your program references a nested table or varray element using an index number (-1 for example) that is outside the legal range.

Exception: SYS_INVALID_ROWID
Oracle Error: ORA-01410
SQLCODE: -1410

The conversion of a character string into a universal rowid fails because the character string does not represent a valid rowid.

Exception: TIMEOUT_ON_RESOURCE
Oracle Error: ORA-00051
SQLCODE: -51

A time-out occurs while Oracle is waiting for a resource.

Exception: TOO_MANY_ROWS
Oracle Error: ORA-01422
SQLCODE: -1422

A SELECT INTO statement returns more than one row.

Exception: VALUE_ERROR
Oracle Error: ORA-06502
SQLCODE: -6502

An arithmetic, conversion, truncation, or size-constraint error occurs. For example, when your program selects a column value into a character variable, if the value is longer than the declared length of the variable, PL/SQL aborts the assignment and raises VALUE_ERROR. In procedural statements, VALUE_ERROR is raised if the conversion of a character string into a number fails. (In SQL statements, INVALID_NUMBER is raised.)

Exception: ZERO_DIVIDE
Oracle Error: ORA-01476
SQLCODE: -1476

Your program attempts to divide a number by zero.

An Example of handling exception (from Oracle Docs)

DECLARE
past_due EXCEPTION;
acct_num NUMBER;
BEGIN
DECLARE ---------- sub-block begins
past_due EXCEPTION; -- this declaration prevails
acct_num NUMBER;
BEGIN
...
IF ... THEN
RAISE past_due; -- this is not handled
END IF;
END; ------------- sub-block ends
EXCEPTION
WHEN past_due THEN -- does not handle RAISE exception
...
END;