Exception in Oracle

本文深入探讨了Oracle中的异常处理机制,包括预定义异常和自定义异常的区别、用法及示例。了解如何在编程中正确处理错误情况,提升应用程序的稳定性和用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值