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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…