none
UnhandledExceptionEventhandler does not catch timer exception and app continues RRS feed

  • Question

  • Hello All !

    From what I read in MSDN [http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx] and in the general part about handling exceptions, since Net 2.0 [and in opposite to the 1.x versions], all unhandled exceptions should terminate the application.
    Also, an "UnhandledExceptionEventHandler" should catch events from all AppDomains.

    Correct me, if I have understood something wrong here!

    I have a console app [so:NO ASP, NO forms], which will become a service one day, and so the same rules should apply. This app has a timer [from  System.Timers], which just dies without any notifications, even I have the "UnhandledExceptionEventHandler" attached.

    Without this handler working, I have to opt to the not recommended way, to wrap all my method bodies with try/catch blocks!

    And help would be really good!

    Best regards,
    scamb
    Sunday, February 21, 2010 1:00 PM

Answers

  •  

    Hi,

    Your understanding about Unhandled Exception of appdomain is correct, the event handler of default appdomain always get invoked if there is any Unhanded Exception.

    From your previous description, you are using System.Timers.Timer, if so, since  System.Timers.Timer will eat all exceptions thrown by registered event handlers of timer's Elapsed event, that's why default appdomain's event handler not called, and you may consider using System.Threading.Timer instead.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 24, 2010 4:10 AM
  • Hi, scamb:
         Exceptions thrown inside a System.Timers.Timer type's Elapsed event handler have been reported to be swallowed. Here is the related link from Microsoft Connect, and there're 2 workrounds for now:
    1. add try/catch block inside the Elapsed event handler
    2. use System.Threading.Timer instead.
        Hope this info could be helpful.
    Please mark the right answer at right time.
    Thanks,
    Sam
    Sunday, March 7, 2010 2:30 PM

All replies

  • UnhandledExceptionEventHandler (http://msdn.microsoft.com/en-us/library/system.unhandledexceptioneventhandler.aspx) catchs only exceptions from 1 AppDomain (where it was registered) - see the first line of MSDN documentation.
    You have likely 2 AppDomains and the exception is thrown from 'the other' AppDomain.

    BTW: You don't need to wrap all method bodies with try/catch, only Main method (and all methods which will be started as new thread's 'main' method. Anyway, it is not recommended as you mentioned.

    -Karel
    Sunday, February 21, 2010 8:41 PM
    Moderator
  • Hi !

    Sorry, I posted the wrong link :-(
    Sorry again: your answer looks incorrect!

    In:
    http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

    MSDN writes:
    "If the UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in. If the thread started in an application domain that has an event handler for UnhandledException , the event is raised in that application domain. If that application domain is not the default application domain, and there is also an event handler in the default application domain, the event is raised in both application domains."

    So, my AppDomain has an handler too and  according to the text above, it should be called!

    Writing apps with a high number of counters gets uncontrollable, if "timers can just do what they want" ... :-(

    So, from my perspective, what I see does not look very professional :-(

    Best regards,
    scamb
    Tuesday, February 23, 2010 7:02 PM
  •  

    Hi,

    Your understanding about Unhandled Exception of appdomain is correct, the event handler of default appdomain always get invoked if there is any Unhanded Exception.

    From your previous description, you are using System.Timers.Timer, if so, since  System.Timers.Timer will eat all exceptions thrown by registered event handlers of timer's Elapsed event, that's why default appdomain's event handler not called, and you may consider using System.Threading.Timer instead.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 24, 2010 4:10 AM
  • Hi,
    I'm writing to check the issue status, please feel free to let us know if you have any concern.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, March 1, 2010 2:10 AM
  • Hello !

    Sorry for my late reply, I had an unplanned traveling ...

    Thanks much, for the clarification. So, first, I am happy to understand the UnhandledException well and it works for me.
    Ok, about the timer:Yes, my problem is really documented in the lib, I had not read this.
    On the other hand:I do not agree with this behavior. IMHO, a thread - even it may be a timerthread - should never swallow exceptions and this should be the default begvaior so far ever possible. But it's just my opionion.

    Thanks!
    Best regards,
    --scamb
    Wednesday, March 3, 2010 9:11 AM
  • Hi,
    If you have any suggestions to Microsoft's products, you may consider posting feedback to https://connect.microsoft.com, the feedback will be routed to the product unit who works on this specific feature area for triage and resolution.
    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, March 3, 2010 10:19 AM
  • Hi, scamb:
         Exceptions thrown inside a System.Timers.Timer type's Elapsed event handler have been reported to be swallowed. Here is the related link from Microsoft Connect, and there're 2 workrounds for now:
    1. add try/catch block inside the Elapsed event handler
    2. use System.Threading.Timer instead.
        Hope this info could be helpful.
    Please mark the right answer at right time.
    Thanks,
    Sam
    Sunday, March 7, 2010 2:30 PM
  • Hi !

    Thanks again!

    Hmm, in that link, I see, the case is open for years and other users reported, that it is event not fixed in Net 4RC2 :-(

    Regards,
    scamb
    Tuesday, March 9, 2010 12:50 PM