When you rethrow an exception, in .NET there's a trap when you use 'throw ex'. You could think that 'throw' and 'throw ex' do exactly the same thing. It's not right, because when you use 'throw ex', you loose precious informations, a part of the original stack trace.
Here is the demonstration :
2:
3: namespace ExApp
4: {
5: class Program
6: {
7: static void Main(string[] args)
8: {
9: try
10: {
11: MyProcessing();
12: }
13: catch (Exception ex)
14: {
15: Console.WriteLine(String.Format("[ERROR] The processing has failed : {0}\n",ex.Message));
16: Console.WriteLine("STACK TRACE : " + ex.StackTrace);
17:
18: }
19: Console.WriteLine("PRESS A KEY TO FINISH");
20: Console.ReadLine();
21: }
22:
23: private static void MyProcessing()
24: {
25: try
26: {
27: MySubProcessing();
28: }
29: catch (Exception ex)
30: {
31: // we should do something here, for example, close a file
32: // or rollback a transaction
33:
34: throw ex;
35: }
36: }
37:
38: private static void MySubProcessing()
39: {
40: throw new ApplicationException("An error has occured");
41: }
42:
43: }
44: }
STACK TRACE :
at ExApp.Program.MyProcessing() in C:\src\ExApp\Program.cs:line 34
at ExApp.Program.Main(String[] args) in C:\src\ExApp\Program.cs:line 11
PRESS A KEY TO FINISH
STACK TRACE :
at ExApp.Program.MySubProcessing() in C:\src\ExApp\Program.cs:line 40
at ExApp.Program.MyProcessing() in C:\src\ExApp\Program.cs:line 34
at ExApp.Program.Main(String[] args) in C:\src\ExApp\Program.cs:line 11
PRESS A KEY TO FINISH
In the first case, we just know that the issue is somewhere in the try block of the MyProcessing function, while in the second case we have the right line in the right function (MySubProcessing line 40).
It's not a bug in C#, it's logical in fact. 'throw ex' throw a new exception while 'throw' rethrow exactly the exception catched previously.
Just be careful with that because it can be a real problem when you loose several levels of stack trace. It's complex to locate an issue in that case.