Catching unhandled ThreadAbortException from Main?
-
Hi, is there any fool proof way to catch unhandled thread exception, whether it's a ThreadAbortException (caused by Thread.Abort from "Main" or UI thread) or a DivisionByZeroException (happenning within the thread function)? I know the following won't catch it:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExHandler);
And I don't think all developers will implement try-catch-finally on thread functions. How do you handle this? I know for WPF app, in App.xaml you can define general handler for unhandled exceptions (But this will NOT catch Thread exceptions). For example, the following will NOT work:private void Application_DispatcherUnhandledException_1(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { string Message = "Unhandled exception: " + e.Exception.ToString(); MessageBox.Show(Message); return; }
dev
-
Hi, is there any fool proof way to catch unhandled thread exception, whether it's a ThreadAbortException (caused by Thread.Abort from "Main" or UI thread) or a DivisionByZeroException (happenning within the thread function)? I know the following won't catch it:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExHandler);
And I don't think all developers will implement try-catch-finally on thread functions. How do you handle this? I know for WPF app, in App.xaml you can define general handler for unhandled exceptions (But this will NOT catch Thread exceptions). For example, the following will NOT work:private void Application_DispatcherUnhandledException_1(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { string Message = "Unhandled exception: " + e.Exception.ToString(); MessageBox.Show(Message); return; }
dev
Well, I allways have a try/catch in my threads. Usualy I catch TreadAbortException and other "most excpected" exceptions and at the end I allways catch general exceptions and log them. In debug mode I tend to insert a Debug.Assert(false) in the general exception handler, to see if I forgot an "expected" exception. In the release they are only logged. Some people say, that you shold not catch general exceptions, but the software I'm used to work on has to run 24/7 and most of the time without a user in front of it. So it has te recover itself after the occurence of any kind of error. I don't know how others manage it, but I don't like software which crashes totally, only showing a messagebox with an error and then say good-bye without any chance of saving data or anything else. Andy
-
Hi, is there any fool proof way to catch unhandled thread exception, whether it's a ThreadAbortException (caused by Thread.Abort from "Main" or UI thread) or a DivisionByZeroException (happenning within the thread function)? I know the following won't catch it:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExHandler);
And I don't think all developers will implement try-catch-finally on thread functions. How do you handle this? I know for WPF app, in App.xaml you can define general handler for unhandled exceptions (But this will NOT catch Thread exceptions). For example, the following will NOT work:private void Application_DispatcherUnhandledException_1(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { string Message = "Unhandled exception: " + e.Exception.ToString(); MessageBox.Show(Message); return; }
dev
In WinForms, there is no way to prevent the termination of an app caused by an unhandled thread exception. AppDomain.UnhandledException will handle it, but will not prevent the app termination. The best you can do in this event handler is to save your data and gracefully exit the program. I always have a top level try catch block in the method directly executed in a thread. This will handle all exceptions thrown from any where down in the call stack.
-
Hi, is there any fool proof way to catch unhandled thread exception, whether it's a ThreadAbortException (caused by Thread.Abort from "Main" or UI thread) or a DivisionByZeroException (happenning within the thread function)? I know the following won't catch it:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExHandler);
And I don't think all developers will implement try-catch-finally on thread functions. How do you handle this? I know for WPF app, in App.xaml you can define general handler for unhandled exceptions (But this will NOT catch Thread exceptions). For example, the following will NOT work:private void Application_DispatcherUnhandledException_1(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { string Message = "Unhandled exception: " + e.Exception.ToString(); MessageBox.Show(Message); return; }
dev
devvvy wrote:
Hi, is there any fool proof way to catch unhandled thread exception,
No because for starters the StackOverflowException cannot be caught. But for the rest, always, always, put a try/catch at the root level of where you start the thread from.
-
devvvy wrote:
Hi, is there any fool proof way to catch unhandled thread exception,
No because for starters the StackOverflowException cannot be caught. But for the rest, always, always, put a try/catch at the root level of where you start the thread from.
-
Hi, is there any fool proof way to catch unhandled thread exception, whether it's a ThreadAbortException (caused by Thread.Abort from "Main" or UI thread) or a DivisionByZeroException (happenning within the thread function)? I know the following won't catch it:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExHandler);
And I don't think all developers will implement try-catch-finally on thread functions. How do you handle this? I know for WPF app, in App.xaml you can define general handler for unhandled exceptions (But this will NOT catch Thread exceptions). For example, the following will NOT work:private void Application_DispatcherUnhandledException_1(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { string Message = "Unhandled exception: " + e.Exception.ToString(); MessageBox.Show(Message); return; }
dev
I would expect some exceptions to be thrown within the threads and some thread related exceptions to be thrown back to the main thread; ThreadAbortException could be one of the later. In the later case, I would expect the following code to catch all exception thrown within the main thread:
/// /// Main entry point. /// \[STAThread\] \[SuppressMessage("Microsoft.Usage", "CA1801")\] // unused parameters public static int Main(string\[\] args) { Program program = new Program(); // winform do { try { program.Update(); // logic code Application.DoEvents(); // updates winform - inputs and drawing } catch (Exception exception) { if (program.Log.IsFatalEnabled) { program.Log.Fatal("Caught Unhandled Exception: " + exception); } break; } } while (program.StateCurrent != EProgramState.Shutdown && program.ExitCode == EProgramError.NoError); return (int)program.ExitCode; }
If ThreadAbortException is thrown within the thread, I would look for a way to register my own thread main method and do similar wrapping in there. Kind Regards, Keld Ølykke
-
devvvy wrote:
Thought I said ThreadAbortException...
You did. You also asked if you could catch all exceptions. The one I posted cannot be caught. The ThreadAbortException (not what I mentioned) is special both it what it does to a thread and what it does on exit from the catch block (excluding special handling.)