I've written about the proper way to re-throw an exception before https://quinngil.com/2022/07/28/exception-throwing/ and now I have an update.
I created myself this extension method to encapsulate this throwing mechanism AND to help ignore the need for the unreachable throw
.
public static class ExceptionExtensions
{
public static Exception ThrowMe(this Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
return new UnreachableException("The compiler just doesn't understand our love.");
}
}
And this is used in the calling catch block as
catch (Exception ex)
{
throw ex.ThrowMe();
}
Since the ThrowMe
throws, the throw
will never be hit. If it is... well... you've got other issues.