I've read How to rethrow InnerException without losing stack trace in C#? and the answer here may be the same; on the other hand my situation is sufficiently different that someone might be able to suggest a better approach.
My simplified structure is
try {
something();
} catch (MyException e1) {
try {
somethingElse();
} catch (MyException e2) {
throw e1;
}
}
That is to say, calling somethingElse()
is an attempt to recover from the original exception, and if the recovery attempt fails, I want to throw the original exception, not the one that arose from the recovery attempt.
I get a warning from the compiler about rethrowing exceptions losing the stack trace (and of course, I don't like leaving my code with warning conditions). But what should I do about it? My IDE (Rider) suggests changing the throw e1;
to throw;
, but that would presumably rethrow e2
.
Are there any solutions short of the convoluted ideas proposed in the cited question for inner exceptions?
To be honest, I don't really care about the imperfect stack trace - I just want to get rid of the warnings.