我有一些delphi代码,有点像这样:
try
//some code
//occasionally throws an exception here, for example an EIndexOutOfRangeException
//more code...should get skipped if exception is thrown
finally
// there may or may not be any important cleanup code here
end;
在这种情况下,除了打破 try 块之外,不需要处理异常。因此,在将 mad- except 添加到项目中进行错误排除之前,这段代码是“有效的”。但现在我收到了错误报告,因为 MadExcept 正在报告未捕获的异常。
相关问题,MadExcept 在 try finally 时触发 https://stackoverflow.com/questions/10746927/madexcept-triggers-on-try-finally表示在这种情况下 MadExcept 中断的行为是“预期的”,因为该异常未被“处理”。
我想澄清一下我的选择是什么,以防止在运行此代码时弹出疯狂异常对话框,无论是否抛出并忽略内部异常。
所以我的想法是正确的,没有开关可以禁用 MadExcept 在 try/finally 块中中断未处理的异常?即使我想忽略它,我也需要明确地“捕获”异常?
我应该做这样的事情(忽略任何例外):
try
//some code
//sometimes throws EIndexOutOfRangeException here
//more code...should get skipped if exception is thrown
except do begin end;
end;
或者也许(忽略一个非常具体的例外):
try
//some code
//sometimes throws EIndexOutOfRangeException here
//more code...should get skipped if exception is thrown
except on E : EIndexOutOfRangeException do begin end;
end;
或者也许需要是:
try
try
//some code
//sometimes throws EIndexOutOfRangeException here
//more code...should get skipped if exception is thrown
except on E : EIndexOutOfRangeException do begin end;
finally
// some cleanup code
end;
如果这三个都是有效的解决方案,我是否应该出于某种原因更喜欢其中一个?