Skip to content

Instantly share code, notes, and snippets.

@the-tatarinov-av
Created August 24, 2017 17:27
Show Gist options
  • Select an option

  • Save the-tatarinov-av/f6d9337ba1c49946beaed15feaa58bfd to your computer and use it in GitHub Desktop.

Select an option

Save the-tatarinov-av/f6d9337ba1c49946beaed15feaa58bfd to your computer and use it in GitHub Desktop.
public static class TaskExtensions
{
public static Task Catch(this Task task, Action<Exception> exceptionHandler)
{
return task.ContinueWith(t =>
{
if (t.IsCanceled || !t.IsFaulted || exceptionHandler == null)
{
return;
}
var innerException = t.Exception?.Flatten().InnerExceptions.FirstOrDefault();
exceptionHandler(innerException ?? t.Exception);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment