Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can an exception in a .NET WinForms app just get eaten without being caught or bubbling up to a windows exception?

In several places in our code, we notice that if running under debugger it'll show that there's an unhandled exception in the code, however if running outside the debugger it will just ignore the exception completely as if it were caught. We have an exception handler that pops up an error submit dialog that's hooked up to Application.ThreadException and AppDomain.CurrentDomain.UnhandledException And neither of those appear to be catching them either. We also log our exceptions and nothing appears in the log.

What are some possible reasons for this?

Edit: It seems that it isn't dependent on the type of exception throw, but rather where it is thrown. This was tested by just adding:

throw new Exception("Test Exception");

It'll show up under debugger but doesn't show up outside, so in our case it's not a ThreadAbortedException or anything that's dependent on it being a specific type of exception.

like image 919
Davy8 Avatar asked Oct 26 '22 07:10

Davy8


2 Answers

There are some special exceptions that don't get bubbled up or caught, it sounds like you're dealing with one of them: see ThreadAbortException

like image 180
Ricardo Villamil Avatar answered Nov 15 '22 07:11

Ricardo Villamil


Found one place where this could occur is if there's an exception in the UnhandledException event handler. An easy way to see this is this: In the Form.Load event handler throw any old exception. In the Application.ThreadException event put something similar to the following:

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
     string b = null;
     int i = b.Length;
}

Under debugger it'll show your exception was unhandled by user code, and then after that it'll show a null reference exception in the ThreadException handler, but if you run it outside the debugger it'll just swallow the exception like it was handled.

like image 38
Davy8 Avatar answered Nov 15 '22 07:11

Davy8