async \ await and Exception Handling

2012/01/12

async \ await and Exception Handling

this post will discuss how async / await is handling exceptions.

async, await, continuation, continue,exception, tpl,.net 4.5, c#5

as we mention in previous post, about the async / await concept, await is all about continuation.

before .NET 4.5 parallel execution exceptions has to be handle in separate of the synchronic handling.

for example:

handling ThreadPool execution:

Code Snippet
  1. void Foo()
  2. {
  3.     try
  4.     {
  5.         Console.WriteLine("Synchronic");
  6.         ThreadPool.QueueUserWorkItem(state =>
  7.             {
  8.                 try
  9.                 {
  10.                     Console.WriteLine("Parallel");
  11.                 }
  12.                 catch (Exception exAsync)
  13.                 {
  14.                     EventLog.WriteEntry("application", exAsync.ToString());
  15.                 }
  16.             }, null);
  17.     }
  18.     catch (Exception ex)
  19.     {
  20.         EventLog.WriteEntry("application", ex.ToString());
  21.     }
  22. }

as you can see we have to handle the parallel exception (line 12) in separate from the synchronic handling (line 18).

TPL has brought new option for handling parallel exception, now you can use ContinueWith with TaskContinuationOptions.OnlyOnFault (line 8).
but still you have to handle the parallel exception is in separate of the synchronic one:

Code Snippet
  1. void Foo()
  2. {
  3.     try
  4.     {
  5.         Console.WriteLine("Synchronic");
  6.         Task t = Task.Factory.StartNew(() => Console.WriteLine("Parallel"));
  7.         t.ContinueWith(tsk => EventLog.WriteEntry("application", tsk.Exception.ToString()),
  8.             TaskContinuationOptions.OnlyOnFaulted);
  9.     }
  10.     catch (Exception ex)
  11.     {
  12.         EventLog.WriteEntry("application", ex.ToString());
  13.     }
  14. }

async / await pattern

using the async / await pattern we can handle both synchronic and parallel exception in the same place:

Code Snippet
  1. async void Foo()
  2. {
  3.     try
  4.     {
  5.         Console.WriteLine("Synchronic");
  6.         await Task.Factory.StartNew(() => Console.WriteLine("Parallel"));
  7.     }
  8.     catch (Exception ex)
  9.     {
  10.         // handling both synchronic and parallel exceptions
  11.         EventLog.WriteEntry("application", ex.ToString());
  12.     }
  13. }

when we are using the async / await pattern at compile time the compiler convert our code into continuation state machine.
therefore the compiler can take the code within the catch area and apply it both for the synchronic and the parallel execution.

Summary

async / await pattern does simplify the exception handling. we do write our exception handling once and it will apply for both synchronic and parallel execution.


kick it on DotNetKicks.com

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

7 comments

  1. fifa 14 full2013/11/18 ב 19:52

    Great post I really enjoyed it!

    Reply
  2. gta 5 pc download2013/11/19 ב 15:35

    Nice post I truly enjoyed it!

    Reply
  3. GTA 5 Download2013/11/19 ב 21:33

    Great post I truly enjoyed it!

    Reply
  4. Assassin's Creed 4 Download2013/11/24 ב 10:51

    Nice article I truly enjoyed it!

    Reply
  5. Music Making Software2013/12/04 ב 19:07

    Wonderful post I really enjoyed it!

    Reply
  6. dor cohen2016/11/20 ב 12:46

    I really enjoy looking through on this web site , it contains good blog posts. “For Brutus is an honourable man So are they all, all honourable men.” by William Shakespeare.

    Reply