[英]Can a try/catch block prevent an error from occuring?
I have some code in my application that throws an error every so often (System.AccessViolationException) - So I wrapped it in a try/catch block and set a debug point and logging method in the catch element. 我的应用程序中有一些经常发生错误的代码(System.AccessViolationException)-因此,我将其包装在try / catch块中,并在catch元素中设置调试点和日志记录方法。 I've found that since I did this the error has stopped happening - the debug point is never hit and nothing is logged. 我发现自从执行此操作以来,该错误已停止发生-永远不会命中调试点,并且不记录任何内容。 As soon as I remove the try from around the code I get the error again. 一旦从代码中删除try,我再次收到错误消息。 What could be causing this? 是什么原因造成的?
The code is pretty trivial: 该代码非常简单:
try
{
var f1 = new ResizeNearestNeighbor(lfu.Width, lfu.Height);
var f2 = new Crop(ViewRectangle);
lfu = f2.Apply(lfu);
lfu = f1.Apply(lfu);
}
catch (Exception ex)
{
MainForm.LogExceptionToFile(ex);//never hit
}
There could be a couple of options: 可能有两种选择:
or MainForm.LogExceptionToFile(ex)
; 或MainForm.LogExceptionToFile(ex)
; does not work as it expected 不能按预期工作
or, if we are 100% sure in that method, probably injecting try/catch
block introduces in the code flow that microscopic delay (due the more IL code to execute/control), which is necessary to not get AccessViolationException
on that machine . 或者,如果我们100%确定该方法,则可能在代码流中注入try/catch
块会引入微观延迟(由于要执行/控制更多的IL代码),这对于在该计算机上不获取AccessViolationException
是必需的。 Which means, that is absolutely possible that you will get that exception on some other machine. 这意味着,这是绝对有可能的,你会得到一些其他的机器上的异常。
A AccessViolation Exception can't be skipped or catched. 不能跳过或捕获AccessViolation异常。
Every time this exception happens it would be thrown! 每当发生此异常时,都将引发该异常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.