oracle自定义异常标量,oracle自定义异常

不声明异常,使用使用raise_application_error抛出只有错误数字(-20999,-20000)和错误信息的异常,不能通过名称捕获,可以通过错误的数字得到自定义的异常信息。

declare

v_bool boolean := true;

begin

if v_bool then

raise_application_error(-20000, 'abc');

else

raise_application_error(-20001, 'def');

end if;

exception

when others then

if sqlcode = -20000 then

dbms_output.put_line('is:abc');

elsif sqlcode = -20000 then

dbms_output.put_line('is:def');

else

dbms_output.put_line(sqlerrm);

end if;

end;

声明一个异常,没有该异常的描述,可以通过名称捕获

declare

e_a exception;

begin

raise e_a;

exception

when e_a then

dbms_output.put_line(sqlerrm);

when others then

dbms_output.put_line(sqlerrm);

end;

exception_init可以将定义的异常名称和一个指定的错误数字关联到一起,通过名称捕获该异常

declare

e_a exception;

pragma exception_init(e_a,-20000);

begin

raise_application_error(-20000,'abc');

exception

when e_a then

dbms_output.put_line('is:abc');

when others then

dbms_output.put_line(sqlerrm);

end;

sqlcode是异常的代码,sqlerrm是异常的信息,dbms_utility.format_error_backtrace返回抛出异常的行。

declare

e_a exception;

begin

raise e_a;

exception

when e_a then

dbms_output.put_line(sqlcode);

dbms_output.put_line(sqlerrm);

dbms_output.put_line(dbms_utility.format_error_backtrace);

end;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30363279/viewspace-2126209/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值