Uncatchable exception terminates C# application

  • Thread starter Thread starter Mek7
  • Start date Start date
M

Mek7

Guest
Hi,

we have a C# application that is actually a worker process for the Azure worker role, only on development PCs it is launched as a console application. The main class is called WorkerRoleEntryPoint and we have configured a general exception handler both in Main() (which is used when lauched on dev PC) and in WorkerRoleEntryPoint() constructor (which is used in Azure environment).

We use a library that we have no control of (ZFramework which is a Entity Framework extension), and this library probably spawns some threads. When something in this library fails, it throws exception which shuts down our worker process. We don't want the process to be shut down, ever. It should be running, only the offending thread should be terminated.

Exception handler is configured like this:

public WorkerRoleEntryPoint()
{
// initialize error handling
AppDomain.CurrentDomain.UnhandledException += this.OnUnhandledException;
}



public static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException2;
}


and later in the code, exception handlers (empty, we just set breakpoints in there):

private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{

}

private static void OnUnhandledException2(object sender, UnhandledExceptionEventArgs e)
{

}

Now when an exception is thrown from the library, control reaches OnUnhandledException2 and stops on the breakpoint:

1579804.png

Note that property IsTerminating = true. When I continue, exception is caught by VS, only stack trace is "External code" this time.

1579805.png

I know that when application reaches the OnUnhandledException2 handler, it's already too late and shutdown of whole application cannot be prevented. But we want to catch the exception and only terminate the offending thread, leaving other threads running. But since the exception comes from a different thread than main code and from a library we have no control of, there's no way of catching it via try/catch (I tried it, didn't work).

What should we do to avoid crash of whole application on unhandled exception? We don't want to use legacyUnhandledExceptionPolicy.

Continue reading...
 
Back
Top