Skip to content

Instantly share code, notes, and snippets.

@MiloszKrajewski
Last active March 26, 2017 18:57
Show Gist options
  • Save MiloszKrajewski/d364d3f4c0162aa88ff934eecc0b0bf1 to your computer and use it in GitHub Desktop.
Save MiloszKrajewski/d364d3f4c0162aa88ff934eecc0b0bf1 to your computer and use it in GitHub Desktop.
// NOTE: this method does not throw it just "prepares to be thrown"
static Exception Rethrow(this Exception ex)
{
try
{
typeof(Exception)
.GetMethod("PrepForRemoting", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(ex, new object[0]);
}
catch (Exception)
{
// this method is about preparing exception to be thrown
// so if it throws exception we just don't care and return
// original exception
}
return ex;
}
// NOTE: this method does throw
static T Rethrow<T>(this T exception)
where T: Exception
{
ExceptionDispatchInfo.Capture(exception).Throw();
// it never actually gets returned, but allows to do some syntactic trick sometimes
return exception;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment