0
votes

Albahari says –

The static TaskScheduler.UnobservedTaskException event provides a final last resort for dealing with unhandled task exceptions. By handling this event, you can intercept task exceptions that would otherwise end the application — and provide your own logic for dealing with them.

Reference: http://www.albahari.com/threading/part5.aspx

@ReedCoopsey says –

Note that UnobservedTaskException won't prevent the app from shutting down in any case - you need to handle your exceptions inside of your task, or always wait on a task, if you want to prevent this from shutting down an app.

Reference: TaskScheduler.UnobservedTaskException never gets called


I read Albahari and registered for UnObservedTaskException and when there was an unhandled exception my application went down with it. I am not sure if I am making any mistake understanding these two threading experts. Can someone tell me if just listening to UnObservedTaskException event and setting the exception as observed is fine to ensure that the application doesn't go down? I do understand that this should be the last resort to handle exceptions and one should rather use a try/catch block or wait.

1

1 Answers

3
votes

According to MSDN the EventArgs for this event has a SetObserved() method that:

Marks the Exception as "observed," thus preventing it from triggering exception escalation policy which, by default, terminates the process.

So both of your sources are kind-of right, when handling the event you have the non-default option to stay alive.