difference between throw and throw ex in c# .net

maverickabhi picture maverickabhi · Feb 13, 2014 · Viewed 29.6k times · Source

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?

Answer

D Stanley picture D Stanley · Feb 13, 2014

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.