DCSIMG
async \ await and Exception Handling - Bnaya Eshet

Bnaya Eshet

Disclaimer

async \ await and Exception Handling

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

Comments

No Comments