Can anyone tell me difference between throw
and throw ex
in brief?
I read that throw
stores previous exceptions, not getting this line.
Can i get this in brief with example?
Yes - throw
re-throws the exception that was caught, and preserves the stack trace. throw ex
throws the same exception, but resets the stack trace to that method.
Unless you want to reset the stack trace (i.e. to shield public callers from the internal workings of your library), throw
is generally the better choice, since you can see where the exception originated.
I would also mention that a "pass-through" catch block:
try
{
// do stuff
}
catch(Exception ex)
{
throw;
}
is pointless. It's the exact same behavior as if there were no try/catch
at all.