⌘BABAKOV.NET Let's code!
100% [dark] [ru]

How to Catch Errors in Oracle (PLSQL) EXCEPTION, SQLERRM, SQLCODE

A small guide to catching errors in Oracle PLSQL.

Description of how to use in Oracle (PLSQL) functions SQLERRM and SQLCODE for catching errors EXCEPTION, with syntax and example.

Function SQLERRM returns an error message related to the last occurred exception (error).

Function SQLERRM has no parameters.

Function SQLCODE returns an error code related to the last occurred exception (error)

Function SQLERRM has no parameters.

Usually, exception handling EXCEPTION looks like this:

EXCEPTION
   WHEN наименование_ошибки_1 THEN
      [statements]

   WHEN наименование_ошибки_2 THEN
      [statements]

   WHEN наименование_ошибки_N THEN
      [statements]

   WHEN OTHERS THEN
      [statements]

END [наименование_процедуры];

You can use functions SQLERRM and SQLCODE to call an error message, for example, like this:

EXCEPTION
   WHEN OTHERS THEN
      raise_application_error(-20001,\'Произошла ошибка - \'||SQLCODE||\' -ERROR- \'||SQLERRM);
END;
-- В данном случае появится всплывающее сообщение.

Or you can save an error message to a table like this:

EXCEPTION
   WHEN OTHERS THEN
      err_code := SQLCODE;
      err_msg := SUBSTR(SQLERRM, 1, 200);

      INSERT INTO error_log_table (error_time, error_number, error_message)
      VALUES (sysdate, err_code, err_msg);
END;
--В данном случае ошибка будет помещена в таблицу error_log_table



Main possible error variants:

DUP_VAL_ON_INDEX
ORA-00001
When trying to perform an INSERT or UPDATE operation that creates a duplicate record violating a unique index (unique index).

TIMEOUT_ON_RESOURCE
ORA-00051
Occurs when a resource being operated on is locked and a timeout occurred.

TRANSACTION_BACKED_OUT
ORA-00061
Transaction rollback occurred.

INVALID_CURSOR
ORA-01001
Trying to use a non-existent cursor. May occur when trying to use FETCH a cursor or CLOSE a cursor before opening it.

NOT_LOGGED_ON
ORA-01012
Trying to perform actions without logging in.

LOGIN_DENIED
ORA-01017
Failed login attempt, access denied, invalid user or password.

NO_DATA_FOUND
ORA-01403
One of the following: Trying to perform an INSERT SELECT INTO with no returned values (select - nothing returns). Trying to access an uninitialized row/record in a table. Trying to read a record after the end of a file using package UTL_FILE.

TOO_MANY_ROWS
ORA-01422
Trying to insert a value into a variable using SELECT INTO and select returned more than one value.

ZERO_DIVIDE
ORA-01476
Trying to divide by zero.

INVALID_NUMBER
ORA-01722
Trying to execute a SQL query that performs a string (STRING) to number (NUMBER) conversion, which is impossible.

STORAGE_ERROR
ORA-06500
Either memory shortage or memory error.

PROGRAM_ERROR
ORA-06501
Internal programming error, recommended to contact Oracle support with this error.

VALUE_ERROR
ORA-06502
Trying to perform a data conversion operation that ended with an error (e.g., rounding, type conversions, etc.).

CURSOR_ALREADY_OPEN
ORA-06511
You are trying to open a cursor that is already open.

That's it.

Comments (1)

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.