There are some posts that asks what the difference between those two are already.
(why do I have to even mention this...)
But my question is different in a way that I am calling "throw ex" in another error god-like handling method.
public class Program {
public static void Main(string[] args) {
try {
// something
} catch (Exception ex) {
HandleException(ex);
}
}
private static void HandleException(Exception ex) {
if (ex is ThreadAbortException) {
// ignore then,
return;
}
if (ex is ArgumentOutOfRangeException) {
// Log then,
throw ex;
}
if (ex is InvalidOperationException) {
// Show message then,
throw ex;
}
// and so on.
}
}
If try & catch
were used in the Main
, then I would use throw;
to rethrow the error.
But in the above simplied code, all exceptions go through HandleException
Does throw ex;
has the same effect as calling throw
when called inside HandleException
?
Yes, there is a difference;
throw ex
resets the stack trace (so your errors would appear to originate fromHandleException
)throw
doesn't - the original offender would be preserved.When you do throw ex, that exception thrown becomes the "original" one. So all previous stack trace will not be there.
If you do throw, the exception just goes down the line and you'll get the full stack trace.
if all Line 1 ,2 and 3 are commented - Output - inner ex
if all Line 2 and 3 are commented - Output - inner ex System.DevideByZeroException: {"Attempted to divide by zero."}---------
if all Line 1 and 2 are commented - Output - inner ex System.Exception: devide by 0 ----
if all Line 1 and 3 are commented - Output - inner ex System.DevideByZeroException: {"Attempted to divide by zero."}---------
and StackTrace will be reset in case of throw ex;
MSDN stands for: