在 pl sql 中的所有异常情况下无论如何都要做些啥吗?

Posted

技术标签:

【中文标题】在 pl sql 中的所有异常情况下无论如何都要做些啥吗?【英文标题】:Is there anyway to do something in all exception cases in pl sql?在 pl sql 中的所有异常情况下无论如何都要做些什么吗? 【发布时间】:2012-04-10 15:07:15 【问题描述】:

我想在发生异常时为变量赋值。

exception
  when excep1 then
    var = true;
  when excep2 then
    var = true;
end;

我想做类似的事情,有可能吗?

exception
   var = true;
   when excep1 then
    -- do something
   when excep2 then
    -- do something
end;

【问题讨论】:

【参考方案1】:

按照 Odi 的建议重新加注肯定会奏效。做一些不同的事情,你会得到同样的效果。

begin
  var := true;

  ... your code that can cause exceptions...

  var := false; --var set to false unless an exception was encountered
exception
  when exception1 then
    ...
  when exception2 then
    ...
end;

【讨论】:

谢谢约翰,真的很棒。【参考方案2】:

您可以使用子块执行此操作,首先设置值,然后重新引发异常以处理它:

  begin
    begin
    -- do something
    exception
      when others then
        var = true;
        raise; -- re-raise the current exception for further exception handling
    end;
  exception
    when excep1 then
      -- do something
    when excep2 then
      -- do something
  end;

【讨论】:

感谢您的回复,奥迪 :)【参考方案3】:

另一种方法是放在另一个过程中,例如:

declare
  procedure common_exception_routine is
  begin
    var = true;
  end common_exception_routine;
begin
  ...
exception
  when excep1 then
    common_exception_routine;
  when excep2 then
    common_exception_routine;
end;

【讨论】:

以上是关于在 pl sql 中的所有异常情况下无论如何都要做些啥吗?的主要内容,如果未能解决你的问题,请参考以下文章