Consider this method (pardon the sad attempt at Chuck Norris humor :) ):
public class ChuckNorrisException : Exception
{
public ChuckNorrisException()
{
}
public ChuckNorrisException(string message)
: base(message)
{
}
public ChuckNorrisException(string message, Exception cause)
: base(message, cause)
{
}
protected ChuckNorrisException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
static void ExceptionTest(double x)
{
try
{
double y = 10 / x;
Console.WriteLine("quotient = " + y);
}
catch (Exception e)
{
e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
e;
throw e;
}
}
In resharper, I get a warning on the "throw e" line saying "Exception rethrow possibly intended". But obviously in this case that s not the intention, since e could be wrapped in ChuckNorrisException, and if I just use "throw", that wrapped exception wouldn t get thrown.
I know I can suppress the resharper warning, but then it will be turned off for all scenarios if I m not mistaken. I just wondered if anybody else had encountered this. The only workaround I ve found is to make another exception variable (e2, for example), and throw that. That may be the best I can do here. Seems like resharper could detect this issue though and be smart enough to know that if e is modified, then throw e is ok.
Thanks.
[EDIT] Sorry, I forgot a step. Before the throw, I need to log the exception, so I can t just do:
e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
e;
throw e;
I have to do:
e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
e;
LogException(e);
throw e;