开发者

Exception handling in pl/sql

I ha开发者_开发知识库ve a stored procedure

create or replace procedure Trial
    is 
Begin
---Block A--
EXCEPTION
when others then
insert into error_log values('error');
--Block A ends----
--Block B ----
----Block B ends---
end;

I want code in Block B to execute in all condition i.e if exception in Block A is raised or not.With the above code Block B executes only if exception is raised. How to do this. Please help.


You can created nested blocks:

create or replace procedure Trial
    is 
Begin
  begin
    ---Block A--
  EXCEPTION
    when others then
      insert into error_log values('error');
  end;
  begin
    --Block B ----
  end;
end;


Please note that it's a common antipattern to catch all exceptions without raising them. You might also want to consider an autonomous transaction in order to keep the error-log after a rollback.

So you'd probably be better off with something like this:

create or replace procedure Trial
is 

  procedure finally is
  begin
    --Block B ----
  end;

  procedure logerr (msg varchar2) is
    PRAGMA AUTONOMOUS_TRANSACTION;
  begin
    insert into error_log values(msg);
    commit;
  end;

Begin
  begin
    ---Block A--
  EXCEPTION
    when others then
      logerr(SQLERRM);
      finally;
      raise;
  end;

  finally;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜