尝试除了最后在Delphi中尝试

Vas*_*lis 1 delphi lazarus fpc

对于Delphi或fpc中的嵌套异常处理,已经提到了很多东西.比如这样的东西.我的问题,也许解决了对嵌套try...块的需求,如果下面两个版本的代码之间存在实际差异,我没有看到任何除非未定义的行为或某事后发生expectfinally...

try
    StrToInt('AA');
finally
    writeln('I absolutely need this');
end;
writeln('and this');
Run Code Online (Sandbox Code Playgroud)

和...

try
  StrToInt('AA');
except
end;
writeln('I absolutely need this');
writeln('and this');
Run Code Online (Sandbox Code Playgroud)

Dal*_*kar 8

是,有一点不同.巨大的一个.

如果try块中没有异常,则两个版本都将执行所有代码,但是如果存在异常行为则不同.

在代码的第一个版本中,finally块之后的任何内容都不会被执行,异常将传播到下一个级别.

try
    StrToInt('AA'); // if this code throws exception and it will
finally
    writeln('I absolutely need this'); // this line will execute
end;
writeln('and this'); // this line will not execute 
Run Code Online (Sandbox Code Playgroud)

在第二个版本中,异常将由except块处理,代码将继续正常执行.

try
  StrToInt('AA'); // if this code throws exception and it will
except
end;
writeln('I absolutely need this'); // this line will execute
writeln('and this'); // this line will also execute
Run Code Online (Sandbox Code Playgroud)

在链接的问题中,您有嵌套的异常块,并且这种情况的行为与上面的情况不同,就像在该问题的答案中解释的那样.


文档:Delphi例外

  • 阅读和理解是两回事.特别是,如果英语不是您的母语.明显的事情可能不那么明显. (3认同)
  • IRL尝试...最终主要用于管理范围分配,与EH无关.一个有趣的事实是,对于您的示例,存在StrtoInt的变体:TryStrToInt,StrToIntDef等. (3认同)
  • FWIW,在第二个代码块中,异常未被处理,只是被吞下.IMO应该强调的是try-finally用于资源保护,而try-except真正意味着处理可以处理的异常(而不仅仅是吞下它们). (2认同)