rethrow for debugging
A question came up on an internal email list.
You will get this error in the following code, as per the rules of C#:
try
{
....
}
catch(Exception e)
{
// Handle cleanup.
throw;
}
With the exception of disabling this specific warning, is there anyway to have the Exception variable in a catch that is not using it (so that debugging is easier) and not hit this warning?
Can the warning be disabled only for catch()? (Yeah, I am guessing it can't too, worth asking. :))
The goal is to make it easy to set a breakpoint here & see the details of the exception, without interfering with the behavior of the program. ‘throw e’ is different from ‘throw’ here.
One proposal was to throw a new exception, with ‘e’ as the inner exception. The proposal relied too much on strings for my taste, so I put this together:
using System;
using System.Runtime.Serialization;
[Serializable]
public class RethrownException : ApplicationException
{
public static void Throw(Exception exception)
{
throw new RethrownException(exception);
}
public RethrownException(Exception inner) : base(null, inner) { }
public RethrownException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
And here’s the usage:
try
{
throw new System.Exception("sdfs");
}
catch (System.Exception ex)
{
RethrownException.Throw(ex);
}
What do you think?