Last active
March 26, 2017 18:57
-
-
Save MiloszKrajewski/d364d3f4c0162aa88ff934eecc0b0bf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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