How to handle Async Exceptions?

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

You probably use Async-methods, but you don’t like the way how exceptions are handled? Remember, the async exception is typically an AggregatedException. In this case you have to dig into the list of exceptions etc., etc.

Following peace of code shows how you can implement a very simple handler method, which grabs the right exception from the list of exceptions.


AsyncExceptionHelper
(() => YourMethodAsync().Result);


   . . .

 public static T AsyncExceptionHelper<T>(Func<T> method){

           T retObj = default(T);
            try
            {
                retObj = method();
            }

            catch (AggregateException exception)
            {
                ExceptionDispatchInfo.Capture(exception.Flatten().InnerException).Throw();

            }

            return retObj;
 }


Posted Jan 27 2015, 07:34 AM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.