Consider the following c# code:
public class ExceptionManager
{
public static void TreatException(Exception ex)
{
if (ShowAndContinue(ex))
// display a user-friendly message on what happened and let the app run.
else
throw ex;
}
// (more code)
}
(Please note that the above is over-simplified so that the focus is on my actual concern)
IMHO I think the way the exception is thrown is wrong because stacktrace information is lost. The preferred way (again, IMHO) to write the throw
line would be this:
throw new UntreatableErrorException("Manager could not treat the exception.", ex);
This way the original exception's stacktrace is preserved.
However, a colleague of mine is arguing that doing so would mean losing the ability to catch the original exception lower in the call stack, because catch
works with exception type and hence an exception trap might be set to catch the original exception but not the UntreatableErrorException
. Therefore it's better to throw back the original exception instance, even if it means losing the stacktrace.
Which one of us is right here on what should be the best practice?