Skip to content

Instantly share code, notes, and snippets.

@michaeljbailey
Created May 12, 2015 18:12
Show Gist options
  • Save michaeljbailey/28e84d2e1e7cc1217d2f to your computer and use it in GitHub Desktop.
Save michaeljbailey/28e84d2e1e7cc1217d2f to your computer and use it in GitHub Desktop.
Unwraps exceptions to the specified exception type
public static class ExceptionExtensions
{
public static TException Unwrap<TException>(this Exception exception) where TException : Exception
{
for (; exception != null; exception = exception.InnerException)
{
var typedException = exception as TException;
if (typedException != null)
{
return typedException;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment