MadExcept +尝试/最终阻止? [英] MadExcept + try/finally block?

查看:82
本文介绍了MadExcept +尝试/最终阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些delphi代码,就像这样:

  try//一些代码//有时会在此处引发异常,例如EIndexOutOfRangeException//更多代码...如果引发异常,则应跳过最后//这里可能有也可能没有任何重要的清理代码结尾; 

在这种情况下,除了打破try块之外,不需要进行其他处理.因此,在将mad-except添加到项目中以进行错误故障排除之前,此代码是有效的".但是现在我收到了错误报告,因为MadExcept正在报告未捕获的异常.

相关问题, MadExcept触发器最终尝试表示MadExcept的行为闯入了这种情况是预期的",因为没有处理"异常.

我想澄清一下我的选项是什么,以防止在运行此代码时弹出异常除外"对话框,而无论是否引发并忽略了内部异常.

因此,我认为没有任何方法可以禁止MadExcept在try/finally块中打破未处理的异常是正确的吗?而且即使我希望忽略该异常,我也需要明确地捕获"该异常?

我应该这样做吗(忽略任何异常):

  try//一些代码//有时会在此处引发EIndexOutOfRangeException//更多代码...如果引发异常,则应跳过除了开始结束;结尾; 

或者也许(忽略一个非常特殊的例外):

  try//一些代码//有时会在此处引发EIndexOutOfRangeException//更多代码...如果引发异常,则应跳过除了在E上:EIndexOutOfRangeException确实开始结束;结尾; 

或者可能需要:

  try尝试//一些代码//有时会在此处引发EIndexOutOfRangeException//更多代码...如果引发异常,则应跳过除了在E上:EIndexOutOfRangeException确实开始结束;最后//一些清理代码结尾; 

如果所有这三个都是有效的解决方案,出于任何原因,我是否应该选择一个而不是另一个?

解决方案

所以我认为没有办法禁止MadExcept在try/finally块中打破未处理的异常是正确的吗?

是的. try/finally 不是异常处理保证清除,无论是否发生异常.因此, try/finally 块与诸如MadExcept之类的异常处理工具完全无关.

即使我希望忽略该异常,我也需要明确地捕获"该异常?

是的.这就是例外的工作方式.他们沿着堆栈向下工作,直到找到一个捕获它们的处理程序.如果不存在这样的处理程序,则OS会将其解释为崩溃并终止程序.Delphi的 TApplication 对象在靠近调用堆栈底部的位置安装了一个处理程序,以使您的程序不会崩溃,MadExcept对此进行钩挂,以便在出现异常时生成报告.

如果您想忽略一个异常,是的,您确实需要捕获它,因为您正在做的事情是正式地通过在堆栈中展开并忽略它来捕获该异常来处理该异常".关于此时在堆栈展开中捕获它"的那部分很重要,因为这意味着堆栈展开在该位置停止,程序恢复正常执行.如果您只是忽略了它(即,在代码中什么都没做,包括没有安装异常处理程序),它将继续将堆栈一直展开到默认处理程序,无论您是否安装了MadExcept.

是的,在这种情况下,示例2是正确的做法.如果此时需要执行清除代码,则示例#3也将有效.但是示例#1 在任何情况下都绝不能做 ,因为这意味着您可能最终会忽略一个 没想到的异常,然后您最终会在程序中遭到破坏,并且永远不会意识到它.

I have some delphi code, kind of like this:

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;

The exception in this case is not something that needs handling beyond breaking out of the try block. So, before mad-except was added to the project for error troubleshooting, this code was "working". But now I'm getting bug reports because MadExcept is reporting the uncaught exception.

Related question, MadExcept triggers on try finally indicates the behavior of MadExcept breaking in such circumstance is "expected" because the exception isn't "handled".

I would like some clarification about what my options are as far ways to prevent a mad-except dialog from popping up when this code runs, regardless of whether there's an internal exception being thrown and ignored.

So I'm correct in thinking there's no switch to disable MadExcept from breaking on unhanded exceptions in a try/finally block? And I'm going to need to explictly "catch" the exception even if I wish to ignore it?

Should I be doing something like this (ignore any exception):

try
  //some code
  //sometimes throws EIndexOutOfRangeException here
  //more code...should get skipped if exception is thrown
except do begin end;
end;

or perhaps (ignore a very specific exception):

try
  //some code
  //sometimes throws EIndexOutOfRangeException here
  //more code...should get skipped if exception is thrown
except on E : EIndexOutOfRangeException do begin end;
end;

or maybe it needs to be:

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;

If all three of those are valid solutions, should I prefer one over the other for any reason?

解决方案

So I'm correct in thinking there's no switch to disable MadExcept from breaking on unhanded exceptions in a try/finally block?

Yes. try/finally is not exception handling; it's guaranteed cleanup, whether or not an exception occurs. As such, try/finally blocks are completely irrelevant to exception handling tools such as MadExcept.

And I'm going to need to explicitly "catch" the exception even if I wish to ignore it?

Yes. That's how exceptions work. They work their way down the stack until they find a handler that catches them. If no such handler exists, the OS interprets it as a crash and terminates the program. Delphi's TApplication object installs a handler very close to the bottom of the call stack so that your program won't crash, and MadExcept hooks this so that if an exception reaches this point, a report will be generated.

If you want to ignore an exception, yes, you do need to catch it, because what you are doing is formally "handling the exception by catching it at this point in the stack unwinding and ignoring it." That part about "catching it at this point in the stack unwinding" is important, since it means that the stack unwinding halts at that point and the program resumes normal execution. If you just ignored it, (ie. did nothing about it in your code, including not installing an exception handler,) it would continue to unwind the stack all the way to the default handler, whether or not you had MadExcept installed.

So yeah, in this case, example #2 is the correct course of action. Example #3 would be valid too, if you have cleanup code that needs to be performed at this point. But example #1 should never be done under any circumstances because it means you might end up ignoring an exception that you were not anticipating, and then you end up with corruption in your program and you never become aware of it.

这篇关于MadExcept +尝试/最终阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆