异常的捕获及传递

内层的块不捕获,外层也不捕获,则异常被传递到外层后被抛出,同样也不会输出1:

 

SQL>  declare

  2         a emp%rowtype;--define a variable

  3       begin

  4          begin

  5           select * into a from emp where empno=0; --此处没有返回行,产生异常

  6           --start trap exception

  7           exception

  8                  --when no_data_found then

  9                  --dbms_output.put_line('no data found!');

 10                   when too_many_rows then

 11                    dbms_output.put_line('to  many rows found'||sqlcode||','||sqlerrm);

 12                   --when others then

 13                    --dbms_output.put_line('others error occur!');

 14          end;

 15          begin

 16          dbms_output.put_line(1);

 17          end;

 18         end;

 19  /

 declare

*

ERROR at line 1:

ORA-01403: no data found

ORA-06512: at line 5

 

在块内不捕获异常,在外层捕获,则捕获传递到外层并被捕获,且外层的块不会输出1:

 

SQL>  declare

  2         a emp%rowtype;--define a variable

  3   begin

  4          begin

  5           select * into a from emp where empno=0; --此处没有返回行,产生异常

  6           --start trap exception

  7           exception

  8                  --when no_data_found then

  9                  --dbms_output.put_line('no data found');

 10                   when too_many_rows then

 11                    dbms_output.put_line('to  many rows found!'||sqlcode||','||sqlerrm);

 12                  --when others then

 13                  --dbms_output.put_line('others error occur!');

 14          end;

 15          begin

 16          dbms_output.put_line(1);

 17          end;

 18             exception

 19             when no_data_found then

 20          dbms_output.put_line('no data found !');        

 21   end;

 22  /

no data found!

 

PL/SQL procedure successfully completed.

 

 

在块内捕获异常,异常不会传递到外层,因此外层的块会输出1:

 

SQL>  declare

  2         a emp%rowtype;--define a variable

  3   begin

  4          begin

  5           select * into a from emp where empno=0; --此处没有返回行,产生异常

  6           --start trap exception

  7           exception

  8                   when no_data_found then

  9                    dbms_output.put_line('no data found!');

 10                   when too_many_rows then

 11                    dbms_output.put_line('to  many rows found!'||sqlcode||','||sqlerrm);

 12                  --when others then

 13                  --dbms_output.put_line('others error occur!');

 14          end;

 15          begin

 16          dbms_output.put_line(1);

 17          end;       

 18   end;

 19  /

no data found!

1

 

PL/SQL procedure successfully completed.

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

转载于:http://blog.itpub.net/10972173/viewspace-709786/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值