2

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

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;
4

3 回答 3

3

按照 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;
于 2012-04-10T19:25:40.533 回答
1

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

  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;
于 2012-04-10T17:09:48.200 回答
1

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

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;
于 2012-04-11T02:09:50.460 回答