locked
Cannot catch exception in a thread RRS feed

  • Question

  • I have an UnhandledException event attached to AppDomain but it does not catch an exception from a background thread. Application simply closes without my handler notifying users. How can I catch such exception? Have read google and here and can not figure out

     

    Tuesday, February 1, 2011 6:27 PM

Answers

All replies

  • If this is a Windows Forms application, exceptions in the main thread need to be handled/logged via Application.ThreadException.

     

    Unhandled exceptions on background threads should trigger AppDomain.UnhandledException.

     

    If you're using the Task Parallel Library, you may also need to listen to TaskScheduler.UnobservedTaskException on the scheduler (probably TaskScheduler.Default) being used.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by CS001 Tuesday, February 1, 2011 8:43 PM
    • Marked as answer by eryang Monday, February 14, 2011 8:16 AM
    Tuesday, February 1, 2011 7:35 PM
  • Unhandled exceptions on background threads should trigger AppDomain.UnhandledException.  But, not always.

    If you have a thread that starts other threads, an exception thrown on one of these secondary threads may not bubble up to the main thread that started it all.  The CLR will simply shut down the application under that circumstance because the exception only gets as far as the primary background thread. 

    I have seen circumstances where this scenario will cause another thread from the ThreadPool to be fired up, instead of the exception bubbling up to the main thread.  This new thread cannot handle it, and causes the CLR to abruptly terminate the application.

    Post code if you need a more detailed reply.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Tuesday, February 1, 2011 9:03 PM
  • Hi sn75,

     

    I assume you mean that your background thread does not throw the exception to the foreground thread.  Is this correct?  You can simply write the code to catch the exception in the background thread and then have the background thread write the exception details and the stack trace to a log file, so at least you could find out more information about what is causing the exception.

     

    To get started on a potential logging solution, you may want to check out this website:

     

    http://logging.apache.org/log4net/

     

    Please let me know if this is helpful, or if you have any other questions.

     

    All the best,

     

    Roger

    Tuesday, February 1, 2011 11:38 PM
  • After doing a little more research, you could also use the System.ComponentModel.BackgroundWorker (cf. http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx).  Then if the background worker experiences an error, it sets the Error property.

    Tuesday, February 1, 2011 11:47 PM
  • Actually what I do is I throw an exception myself which I expected to bubble up to the main thread where I show the message to the user before the application shuts down.

    I am using WPF if that matters and in this particular case running a simple BackgroundWorker. However it is not about errors in the Background worker because the application shuts down without me being able to interfere in the process. All I need is to show a message with a digestable explanation for shutdown

    Wednesday, February 2, 2011 11:08 AM
  • Exceptions do not "bubble" from one thread to another.  The bubble bursts.  Exceptions "bubble" up the Call Stack.  Each thread has its' own Call Stack. 

    If a background thread does not catch it, the application shuts itself down.  However, a thread can catch an exception and notify another thread about what occured. 

    http://msdn.microsoft.com/en-us/library/system.componentmodel.asyncoperation.aspx  See the Post method.

    Post code if you need a more detailed reply.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Marked as answer by eryang Monday, February 14, 2011 8:16 AM
    Wednesday, February 2, 2011 11:03 PM