Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created October 21, 2012 02:47
Show Gist options
  • Save nakamura-to/3925528 to your computer and use it in GitHub Desktop.
Save nakamura-to/3925528 to your computer and use it in GitHub Desktop.
Rethrow the inner exception of a TargetInvocationException with .NET 4.5 ExceptionDispatchInfo
class Program
{
static void Main(string[] args)
{
var method = typeof (Program).GetMethod("Hello");
try
{
method.Invoke(null, new object[] {});
} catch(TargetInvocationException e)
{
var info = ExceptionDispatchInfo.Capture(e.InnerException);
info.Throw();
}
}
public static void Hello()
{
throw new Exception();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment