Vas*_*lis 1 delphi lazarus fpc
对于Delphi或fpc中的嵌套异常处理,已经提到了很多东西.比如这样的东西.我的问题,也许解决了对嵌套try...块的需求,如果下面两个版本的代码之间存在实际差异,我没有看到任何除非未定义的行为或某事后发生expect或finally...
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)
是,有一点不同.巨大的一个.
如果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例外
| 归档时间: |
|
| 查看次数: |
827 次 |
| 最近记录: |