Caught exception not captured and discarded in throw


RH1003 Warning

Cause

A caught exception is not captured as a local variable, nor used 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

Capture the exception as a local variable and use it as an inner exception argument when constructing a new exception.

Examples

Violates

void ThrowDiscardingException()
{
	try 
	{	
		Method();
	} catch
	{
		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()   
/*