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.
[@more@]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;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9896745/viewspace-933533/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PL/SQL 宣告SQL
- Oracle PL/SQLOracleSQL
- pl/sql to_dateSQL
- PL/SQL 運算子SQL
- PL/SQL 條件SQL
- PL/SQL 迴圈SQL
- PL/SQL 遊標SQL
- Oracle PL/SQL塊簡介OracleSQL
- ultraedit高亮顯示pl/sqlSQL
- Oracle 的PL/SQL語言使用OracleSQL
- PL/SQL 條件控制語句SQL
- PL/SQL程式設計急速上手SQL程式設計
- [20240607]PL/SQL中sql語句的註解.txtSQL
- 使用profiler測試Oracle PL/SQL效能OracleSQL
- Oralce之PL/SQL程式設計(遊標)SQL程式設計
- OCP 複習筆記之PL/SQL (1)筆記SQL
- Oracle PL/SQL程式碼中的註釋OracleSQL
- PL/SQL第二章--基本語法SQL
- PL/SQL第三章--游標SQL
- OCP 複習筆記之PL/SQL (3)筆記SQL
- 6.4. PL/SQL語法——6.4.7. 集合SQL
- pl/sql developer的一個小問題SQLDeveloper
- OCP 複習筆記之PL/SQL (2)筆記SQL
- OCP 複習筆記之PL/SQL (4)筆記SQL
- OCP 複習筆記之PL/SQL (5)筆記SQL
- ORA-06544:PL/SQL:internal error,arguments:[56319]SQLError
- [Bash] Error handlingError
- Oracl資料庫+PL/SQL安裝與配置資料庫SQL
- PL/SQL Developer連線到Oracle 12cSQLDeveloperOracle
- openGauss關於PL/SQL匿名塊呼叫測試SQL
- 【OracleEBS】 在PL/SQL中呼叫Oracle ERP請求OracleSQL
- 6.4. PL/SQL語法——6.4.6. 遊標SQL
- 如何在PL/SQL中讀寫檔案(轉)SQL
- 「Oracle」客戶端 PL/SQL DEVELOPER 安裝使用Oracle客戶端SQLDeveloper
- 【TUNE_ORACLE】列出LOOP套LOOP的PL/SQL程式碼SQL參考OracleOOPSQL
- Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQLMySqlJDBCExceptionError
- PL/SQL 連線 Oralce 提示 Could not initialize oci.dllSQL
- PL/SQL Developer下載地址和漢化包地址SQLDeveloper
- Oracle vs PostgreSQL Develop(23) - PL(pg)sql(引數宣告)OracleSQLdev