Your approach is correct. However, you will not be able to stop your application from terminating.
When an unhandled exception is thrown on a thread you have created in your application CurrentDomain_UnhandledException
will be called allowing you to log or report the exception. However, unless the e.IsTerminating
is false
you will not be able to stop your application from terminating. You can read more about this behavior in Exceptions in Managed Threads.
If you find that CurrentDomain_UnhandledException
is never called you should verify that Application_Startup
is called to setup the handler.
If you still are having problems you should verify that Utilities.DisplayUnhandledException
does not throw an exception. This will also lead to immediate termination of your application. In particular, if e.ExceptionObject
is not of type Exception
casting it to Exception
will throw an exception. However, under normal circumstances, when the exception object is not a managed exception, it will be wrapped in a RuntimeWrappedException
.
To avoid termination of your application you need to catch the exception at "the top of the stack" of your thread method. If that is not possible because you do not have access to the code then the unhandled exceptions are an indication of buggy software, and even though it is inconvenient the best thing to do when a software bug is discovered is to terminate the application to avoid corruption.