ITPub博客

首页 > Linux操作系统 > Linux操作系统 > PLSQL声明部分异常捕获

PLSQL声明部分异常捕获

原创 Linux操作系统 作者:yangtingkun 时间:2012-09-18 23:10:57 0 删除 编辑

近期在看PL/SQL的文档,发现了很多有趣的小知识点,有的以前知道,也有很多以前并不了解的,写出来和大家分享一下。

这篇描述异常捕获的作用范围。

 

 

PL/SQL的异常捕获只针对执行部分,在声明部分产生的异常是无法捕获的:

SQL> set serverout on size 100000
SQL> declare
2 v_num number;
3 begin
4 v_num := 'a';
5 exception
6 when others then
7 dbms_output.put_line('Exception captured!');
8 end;
9 /
Exception captured!

PL/SQL procedure successfully completed.

SQL> declare
2 v_num number := 'a';
3 begin
4 null;
5 exception
6 when others then
7 dbms_output.put_line('Exception captured!');
8 end;
9 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2

想要解决声明部分的异常捕获,解决方法是在外面嵌套一层,在外层进行异常的捕获:

SQL> begin
2 declare
3 v_num number := 'a';
4 begin
5 null;
6 exception
7 when others then
8 dbms_output.put_line('Exception captured!');
9 end;
10 exception
11 when others then
12 dbms_output.put_line('Outer exception captured!');
13 end;
14 /
Outer exception captured!

PL/SQL procedure successfully completed.

除了声明部分,异常处理部分本身导致的异常同样也是无法捕获的,解决方法和声明部分一样,需要在外层进行捕获。

 

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

下一篇: ORA-7445(kghfrf)错误
请登录后发表评论 登录
全部评论
暂无介绍

注册时间:2007-12-29

  • 博文量
    1964
  • 访问量
    11076237