Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Catching unhandled ThreadAbortException from Main?

Catching unhandled ThreadAbortException from Main?

Scheduled Pinned Locked Moved C#
wpfcsharpdesigntutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    devvvy
    wrote on last edited by
    #1

    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

    A L J K 4 Replies Last reply
    0
    • D devvvy

      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

      A Offline
      A Offline
      Andy411
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • D devvvy

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D devvvy

          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

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          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.

          D 1 Reply Last reply
          0
          • J jschell

            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.

            D Offline
            D Offline
            devvvy
            wrote on last edited by
            #5

            Thought I said ThreadAbortException...

            dev

            J 1 Reply Last reply
            0
            • D devvvy

              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

              K Offline
              K Offline
              Keld Olykke
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • D devvvy

                Thought I said ThreadAbortException...

                dev

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                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.)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups