Errors occur during runtime processing due to either hardware or network failure or application logic errors are known as exception.
Types of Exceptions in Oracle
Predefined Oracle Exception
User-Defined Exception
Predefined Oracle Exceptions are a part of the package and need not be explicitly written or declared. These exceptions are called if any oracle violation is overridden.
Example:
ORA-06530- ACCESS_INTO_NULL
ORA-06511- CURSOR_ALREADY_OPEN
User-Defined Exception is declared and written by the user himself depending on the need. They need to be raised explicitly using the RAISE command and declared in declaration section.
Following is the predefined oracle exceptions
No_data_found
Too_many_rows
Zero_divide
Login_denied
Program_error
Timeout_on_resource
Invalid_cursor
Cursor_already_open
Dup_val_on_index
A user can explicitly raise an exception by using the Raise command
Oracle is not aware of these exceptions unless declared.
Example:
DECLARE
Trans EXCEPTION;
BEGIN
IF TO_CHAR(SYSDATE, ‘DY’)= ‘SUN’ THEN
RAISE Trans;
END IF
EXCEPTION
WHEN trans THEN
DBMS_OUTPUT.PUT_LINE(‘No transactions today’);
END;
How PL/SQL Exceptions Are Raised?
The PL/SQL exceptions can be raised in 4 ways. Either the user can raise them or they can be raised by the PL/SQL engine. They are as follows:
The PL/SQL runtime engine raised named system exception: These exceptions are raised automatically by the program. You cannot control when PL/SQL will raise a system exception.
The programmer raised named exception: The programmer can use an explicit call to the RAISE statement to raise a programmer-defined or system-named exception.
The programmer raised unnamed, programmer-defined exception: These are raised with an explicit call to the RAISE_APPLICATION_ERROR procedure in the DBMS_STANDARD package.
The programmer re-raised "current" exception: From within an exception handler, you can re-raise the same exception for propagation to the enclosing block
本文深入探讨了Oracle中的异常处理机制,包括预定义异常和自定义异常的区别、用法及示例。了解如何在编程中正确处理错误情况,提升应用程序的稳定性和用户体验。
1429

被折叠的 条评论
为什么被折叠?



