Is there a difference between “throw” and “throw ex”?

前端 未结 10 2362
夕颜
夕颜 2020-11-22 01:08

There are some posts that asks what the difference between those two are already.
(why do I have to even mention this...)

But my question is different in a way

10条回答
  •  迷失自我
    2020-11-22 01:28

    (I posted earlier, and @Marc Gravell has corrected me)

    Here's a demonstration of the difference:

    static void Main(string[] args) {
        try {
            ThrowException1(); // line 19
        } catch (Exception x) {
            Console.WriteLine("Exception 1:");
            Console.WriteLine(x.StackTrace);
        }
        try {
            ThrowException2(); // line 25
        } catch (Exception x) {
            Console.WriteLine("Exception 2:");
            Console.WriteLine(x.StackTrace);
        }
    }
    
    private static void ThrowException1() {
        try {
            DivByZero(); // line 34
        } catch {
            throw; // line 36
        }
    }
    private static void ThrowException2() {
        try {
            DivByZero(); // line 41
        } catch (Exception ex) {
            throw ex; // line 43
        }
    }
    
    private static void DivByZero() {
        int x = 0;
        int y = 1 / x; // line 49
    }
    

    and here is the output:

    Exception 1:
       at UnitTester.Program.DivByZero() in \Dev\UnitTester\Program.cs:line 49
       at UnitTester.Program.ThrowException1() in \Dev\UnitTester\Program.cs:line 36
       at UnitTester.Program.TestExceptions() in \Dev\UnitTester\Program.cs:line 19
    
    Exception 2:
       at UnitTester.Program.ThrowException2() in \Dev\UnitTester\Program.cs:line 43
       at UnitTester.Program.TestExceptions() in \Dev\UnitTester\Program.cs:line 25
    

    You can see that in Exception 1, the stack trace goes back to the DivByZero() method, whereas in Exception 2 it does not.

    Take note, though, that the line number shown in ThrowException1() and ThrowException2() is the line number of the throw statement, not the line number of the call to DivByZero(), which probably makes sense now that I think about it a bit...

    Output in Release mode

    Exception 1:

    at ConsoleAppBasics.Program.ThrowException1()
    at ConsoleAppBasics.Program.Main(String[] args)
    

    Exception 2:

    at ConsoleAppBasics.Program.ThrowException2()
    at ConsoleAppBasics.Program.Main(String[] args)
    

    Is it maintains the original stackTrace in debug mode only?

提交回复
热议问题