Writing 'throw ex;' is almost always wrong!
So what is the difference between throw ex;
and throw;
in C#? The first statement recreates the stack trace while the latter one preserves it. Consider the following method, which will throw an exception if it gets called:
1 | private static void Inner() |
And then you have two different methods implementing the two different coding styles:
1 | private static void Rethrow() |
At last, you have a short console application to call both methods and print their stack trace:
1 | static void Main(string[] args) |
Here is the output:
As you can see, throw;
preserves the actual location, where the exception really was thrown.
There is hardly any case where you want to hide the actual location of an error, so the best is to just use throw;
as your default in a catch clause.