Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

c# - DivideByZeroException being thrown as FatalExecutionEngineError, not caught

My C# (.Net Framework 4.6.2) application was crashing with no logs. After some days of trial and error I found that the source seems to be a simple divide by zero bug. However, that section of code is in a try-catch, but instead of being thrown and caught, the debugger (Visual Studio 2019 v16.2.5) is showing an uncaught "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.

After continuing, this exception shows and then the application process dies.

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

This seems to happen even when no debugger is attached.

Very strange. Is there a way to catch this type of exception? What might be causing the DivideByZeroException to not be caught?

Here is the section of code:

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

Update. Some helpful comments by @gravity and @KlausGütter made me wonder if the issue would still happen if the bmp.Width variable was replaced with a fixed number. It does.

Exception thrown on this line

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Visual Studio the default settings suppress certain exceptions from breaking at the point they're thrown.

The way to catch ALL exceptions is to change this default setting in the Exceptions window in Visual Studio from this:

enter image description here

to this:

enter image description here

This doesn't "always" help but it's a good start because the call stack is more immediate when you break on the line that is actually throwing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...