首页 > 解决方案 > DivideByZeroException 被抛出为 FatalExecutionEngineError,未捕获

问题描述

我的 C# (.Net Framework 4.6.2) 应用程序在没有日志的情况下崩溃。经过几天的反复试验,我发现来源似乎是一个简单的除以零错误。但是,该部分代码在 try-catch 中,但调试器(Visual Studio 2019 v16.2.5)没有被抛出和捕获,而是显示未捕获的“FatalExecutionEngineError”。

Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0xf7bee845, on thread 0x760c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

继续后,显示此异常,然后应用程序进程终止。

System.ExecutionEngineException: 'Exception of type 'System.ExecutionEngineException' was thrown.

即使没有附加调试器,这似乎也会发生。

很奇怪。有没有办法捕捉这种类型的异常?什么可能导致 DivideByZeroException 未被捕获?

这是代码部分:

{
...
///line that causes the exception when 'amount' is zero
int toScaleToWidth = (int)(bmp.Width / amount), toScaleToHeight = (int)(bmp.Height / amount);
...
}
catch(Exception ee)
{
//logging
}

更新。@gravity 和 @KlausGütter 的一些有用的评论让我想知道如果将 bmp.Width 变量替换为固定数字,问题是否仍然会发生。确实如此。

此行抛出异常

标签: c#

解决方案


在 Visual Studio 中,默认设置会抑制某些异常在引发时中断。

捕获所有异常的方法是在 Visual Studio 的“异常”窗口中更改此默认设置:

在此处输入图像描述

对此:

在此处输入图像描述

这并不“总是”有帮助,但它是一个好的开始,因为当您在实际抛出的行上中断时调用堆栈会更直接。


推荐阅读