我想在发生异常时为变量赋值。
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;
答案 0 :(得分: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;
答案 1 :(得分: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;
答案 2 :(得分: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;