oracle存储过程调用中的异常

1.问题描述

oracle存储过程在相互调用(外层调用内层)时,如果内层存储过程出现异常,则内层抛异常语句后面的语句不会继续执行,外层也会抛出异常且后面的语句也不会执行。

如果内层存储过程对异常进行了捕获,外层就不会抛出异常并正常执行。

2.测试案例

内层存储过程:

create or replace procedure exception_test2 is

    v_result number;

    o_result_info varchar2(2000);

begin 

    select 1/0 into v_result from dual;

    dbms_output.put_line('hello world!');

    exception

    when others then rollback;

    o_result_info:='处理失败'||sqlerrm(sqlcode);

    commit; 

    dbms_output.put_line(o_result_info);

end exception_test2;

输出结果:处理失败ORA-01476: 除数为 0


外层存储过程:

create or replace procedure exception_test1 is

  v_error_info varchar2(2000);

  cursor test_cursor is select 1 from dual;                  --创建游标

  var_num number;

begin

  open test_cursor;                                                    --打开游标

  fetch test_cursor into var_num;                              --使用游标

  exception_test2;                                                      --调用内层存储过程

  close test_cursor;                                                   --关闭游标

  dbms_output.put_line('hello world!');

  exception                                                                --捕获异常

    when others then rollback;

    v_error_info:='处理失败'||sqlerrm(sqlcode);

    commit;

    dbms_output.put_line(v_error_info);                   --输出异常信息

end exception_test1;

输出结果:1

处理失败ORA-01476: 除数为 0

hello world!

你可能感兴趣的:(oracle存储过程调用中的异常)