Caught exception discarded in throw
RH1002 Warning
Cause
A caught exception is not captured as an inner exception when a new exception is thrown.
Reason for rule
Not passing on the caught exception can lead to information loss, making debugging harder.
How to fix violations
Use the caught exception as inner exception argument when constructing a new exception.
Examples
Violates
void ThrowDiscardingException()
{
try
{
Method();
} catch (Exception e)
{
throw new Exception("ThrowDiscardingException");
}
}
/* Constructing stack trace after Method has thrown
System.Exception: ThrowDiscardingException
at ThrowDiscardingException()
/*
Does not violate
void ThrowWithInnerException()
{
try
{
Method();
} catch (Exception e)
{
throw new Exception("ThrowWithInnerException", e);
}
}
/* Constructing stack trace after Method has thrown
System.Exception: ThrowWithInnerException ---> System.Exception: Executing Method
at Method()
at ThrowWithInnerException()
--- End of inner exception stack trace ---
at ThrowWithInnerException()
/*