DivideByZeroException being thrown as FatalExecutionEngineError, not caught

-2

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

c#
asked on Stack Overflow May 26, 2020 by Sugrue • edited May 26, 2020 by Sugrue

1 Answer

1

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.

answered on Stack Overflow May 26, 2020 by IVSoftware

User contributions licensed under CC BY-SA 3.0