Geeks With Blogs

News Hi, my name is Vincent Grondin and I'm a senior consultant at Fujitsu, a consulting firm in Montreal, Québec. I'm starting that blog to share some of my thoughts and knowledge on .NET architectures and code. Being a consultant in the .NET world I come across many different things. Some good, some bad and others that are worth a blog post here whether they be good, or hmmm... shall I say, less good :) I hope you enjoy yourself while learning new stuff. Feel free to leave a comment or contact me anytime.
Vincent Grondin

Tasks are very impressive once you manage to wrap your head around a few concepts.  One subject I’d like to cover in this post is how to deal with exceptions in tasks.  There are a few pitfalls one must not fall into when dealing with exception handling in tasks.  First you must remember that each task is responsible for its own error checking or error handling.  Tasks that do not handle their exceptions will crash your application.  How can you avoid that?  Continuations to the rescue!  There is a concept called Continuations in Tasks and they can easily be implemented to help you deal with exactly that type of problem.  All you have to do is “Continue” your task once it's done executing.  The continuation code (typically an Action<Task>) is the right place to check if an exception happened during the execution of the original task.  There comes the concept of “Observing” a task.  A task is deemed “Observed” if you check for it’s Task.Exception property.  For example,  look at the code below where task1 is continued so that exceptions can be caught.  Inside the continuation, you check to see if an exception occurred in task1 like follows:

 

var task1 = Task.Factory.StartNew(() =>

{

    throw new MyCustomException("Task1 faulted.");

})

.ContinueWith( (originalTask) =>

    {

        if(originalTask.Exception != null)

            {             

                                Console.WriteLine("I have observed a " + originalTask.Exception.Tostring());

}

    });

 

Task1 is now Observed and will not crash your application when it throws the exception.  Would there be other alternatives to this?  Yes, a better way of handling the above would be to pass in the OnlyOnFaulted option to the continuation task (and skip the null check) so the continuation would only happen when the original task is faulted.  Another way would be to NOT use the Continuation at all and having previously hooked your code to the TaskScheduler.UnobservedTaskException global event hanler .  This event is your last chance to log the exception before your application possibly crashes.  Of course, for the application not to crash you'll need to call the SetObserved method on the UnobservedTaskExceptionEventArgs parameter of the event.  Typically I recommand that you use Continuations on each tasks AND hook up to this "Global Task Handler" as an additionnal safety net.

Happy coding all !

 

Posted on Saturday, March 30, 2013 7:46 AM | Back to top


Comments on this post: Exception handling when using Tasks

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Vincent Grondin | Powered by: GeeksWithBlogs.net