finally 块中的异常
Exception in finally block
如果在 finally 代码块中引发异常,finally 块的其余部分是否执行?
try
statementList1
finally
command;
command_that_raises;
critical_command;
end
critical_command
会被执行吗?
Manual只谈异常,不谈代码的执行:
If an exception is raised but not handled in the finally clause, that exception is propagated out of the try...finally statement, and any exception already raised in the try clause is lost. The finally clause should therefore handle all locally raised exceptions, so as not to disturb propagation of other exceptions.
查看以下确认:
procedure TForm6.Button1Click(Sender: TObject);
begin
try
ShowMessage('begin');
finally
ShowMessage('enter');
raise Exception.Create('raise');
ShowMessage('end');
end;
end;
现在这个案例:
procedure RaiseAndContinue;
begin
try
raise Exception.Create('raise');
except
end;
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
try
ShowMessage('begin');
finally
ShowMessage('enter');
RaiseAndContinue;
ShowMessage('end');
end;
end;
简短的回答是:除非您处理该异常,否则否代码将不会执行。
如果在 finally 代码块中引发异常,finally 块的其余部分是否执行?
try
statementList1
finally
command;
command_that_raises;
critical_command;
end
critical_command
会被执行吗?
Manual只谈异常,不谈代码的执行:
If an exception is raised but not handled in the finally clause, that exception is propagated out of the try...finally statement, and any exception already raised in the try clause is lost. The finally clause should therefore handle all locally raised exceptions, so as not to disturb propagation of other exceptions.
查看以下确认:
procedure TForm6.Button1Click(Sender: TObject);
begin
try
ShowMessage('begin');
finally
ShowMessage('enter');
raise Exception.Create('raise');
ShowMessage('end');
end;
end;
现在这个案例:
procedure RaiseAndContinue;
begin
try
raise Exception.Create('raise');
except
end;
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
try
ShowMessage('begin');
finally
ShowMessage('enter');
RaiseAndContinue;
ShowMessage('end');
end;
end;
简短的回答是:除非您处理该异常,否则否代码将不会执行。