There's a bit more subtlety to rethrowing exceptions than most developers realize. Although this topic is covered very nicely at The .NET Guy blog, here's another example:
This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2004/07/rethrowing-exceptions.html
does NOT preserve the existing exception call stack (eg the stack will start with this line of code)
DOES preserve the existing exception call stack (eg this code will not appear anywhere in the stack)
In .Net however, when you do “throw ex;”, ex is being re-thrown, but the original stack trace info gets overriden.
Niktu, that’s exactly what I clarified in the comment above from Feb 2005…
Ah, yes …
But did You point it out that using “Throw” doesn’t yeld exactly same results as not catching exception in first place?
(in call stack the line number from try block generating exception gets replaced by line number of “Throw” statement - that could be significant loss of information if you happen to call same metod in multiple places of that try block … as you could read in comments of my first link
Distinction between throw and throw ex is very helpful to me. I was not aware of this difference!
There’s no distinction between throw and throw ex - it’s just that Throw is a verb that needs an object, and ex happens to be the exception object (from Catch ex as exception).
With catch x then throw ex, if you picture ex as a ball, it would be caught, then thrown again.
With throw new exception(text,ex), the ex ball will be caught, stuck inside an even larger ball with text written on it, then thrown again.