为什么在catch块中“throw”和“throw ex”的行为方式相同?

时间:2013-02-04 03:50:13

标签: c#

我读到了在catch块中,我可以使用“throw”重新抛出当前异常。或“扔出前”。

来自:http://msdn.microsoft.com/en-us/library/ms182363%28VS.80%29.aspx

“要保留原始堆栈跟踪信息的异常,请使用throw语句而不指定异常。”

但是当我尝试使用

        try{
            try{
                try{
                    throw new Exception("test"); // 13
                }catch (Exception ex1){
                    Console.WriteLine(ex1.ToString());
                    throw; // 16
                }
            }catch (Exception ex2){
                Console.WriteLine(ex2.ToString()); // expected same stack trace
                throw ex2; // 20
            }
        }catch (Exception ex3){
            Console.WriteLine(ex3.ToString());
        }

我得到三个不同的堆栈。我期待第一个和第二个跟踪是相同的。我究竟做错了什么? (或理解错误?)

System.Exception:test    在C:\ Program.cs:第13行的ConsoleApplication1.Program.Main(String [] args)中 System.Exception:test    在C:\ Program.cs:第16行的ConsoleApplication1.Program.Main(String [] args)中 System.Exception:test    在C:\ Program.cs中的ConsoleApplication1.Program.Main(String [] args):第20行

3 个答案:

答案 0 :(得分:7)

throw只会保留堆栈帧,如果你不从当前帧中抛出它。你只需要用一种方法完成所有这些操作就可以了。

请参阅此答案:https://stackoverflow.com/a/5154318/1517578

PS:提出一个实际上是有效问题的问题+1。

答案 1 :(得分:2)

Simon打我回答,但你可以通过从另一个函数中抛出原始异常来看到预期的行为:

static void Main(string[] args)
{
    try
    {
        try
        {
            try
            {
                Foo();
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
                throw;
            }
        }
        catch (Exception ex2)
        {
            Console.WriteLine(ex2.ToString()); // expected same stack trace
            throw ex2;
        }
    }
    catch (Exception ex3)
    {
        Console.WriteLine(ex3.ToString());
    }
}

static void Foo()
{
    throw new Exception("Test2");
}

答案 2 :(得分:2)

我在这个问题上挖了更多一点,这是我个人的结论:

  

永远不要使用“throw;”但总是重新抛出一个新的Exception   原因指明。

这是我的推理:

我受到了我以前使用Java的经验的影响,并期望C#throw与Java非常相似。我在这个问题上挖了更多,这是我的观察结果:

    static void Main(string[] args){
        try {
            try {
                throw new Exception("test"); // 13
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
                throw ex;// 17
            }
        } catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }

收率:

System.Exception: test
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 13
System.Exception: test
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 17

直观地说,Java程序员本来希望两个例外都是相同的:

System.Exception: test
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 13

但是C#文档明确指出这是预期的结果:

“如果通过在throw语句中指定异常来重新抛出异常,则会在当前方法重新启动堆栈跟踪,并且抛出异常的原始方法与当前方法之间的方法调用列表将丢失。要保留包含异常的原始堆栈跟踪信息,请使用throw语句而不指定异常。“

现在,如果我稍微改变了测试(替换throw ex;通过throw;在第17行)。

        try {
            try {
                throw new Exception("test"); // 13
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
                throw;// 17
            }
        } catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }

收率:

System.Exception: test
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 13
System.Exception: test
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 17

显然这不是我的预期(因为这是原始问题)。我在第二个堆栈跟踪中丢失了原始投掷点。西蒙怀特黑德把这个解释联系起来,这就是扔;仅当在当前方法中未发生异常时才保留堆栈跟踪。因此,在同一方法中没有参数的“抛出”是没有用的,因为一般来说,它无法帮助您找到异常的原因。

做任何Java程序员会做的事情,我将第17行的陈述替换为:

throw new Exception("rethrow", ex);// 17

收率:

System.Exception: test
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 13

System.Exception: rethrow ---> System.Exception: test
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 13
  --- End of inner exception stack trace ---
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 17

这是一个更好的结果。

然后,我开始使用方法调用进行测试。

    private static void throwIt() {
        throw new Exception("Test"); // 10
    }

    private static void rethrow(){
        try{
            throwIt(); // 15
        } catch (Exception ex) {
            Console.WriteLine(ex.ToString());
            throw; // 18
        }
    }

    static void Main(string[] args){
        try{
            rethrow(); // 24
        } catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }

同样,堆栈跟踪不是我(Java程序员)所期望的。在Java中,两个堆栈都是相同的,并且三个方法深入:

java.lang.Exception: Test
    at com.example.Test.throwIt(Test.java:10)
    at com.example.Test.rethrow(Test.java:15)
    at com.example.Test.main(Test.java:24)

第一个堆栈跟踪只有两个方法深度。

System.Exception: Test
  at ConsoleApplication1.Program.throwIt() in Program.cs:line 10
  at ConsoleApplication1.Program.rethrow() in Program.cs:line 15

就好像堆栈跟踪是作为堆栈展开过程的一部分填充的。如果我要记录堆栈跟踪以在那时调查异常,我可能会遗漏重要信息。

第二个堆栈跟踪是三个方法深,但是第18行(throw;)出现在其中。

System.Exception: Test
  at ConsoleApplication1.Program.throwIt() in Program.cs:line 10
  at ConsoleApplication1.Program.rethrow() in Program.cs:line 18
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 24

这个观察结果类似于前面的观察结果:当前方法范围没有保留堆栈跟踪,并且我再次松开发生异常的被调用方法。例如,如果将rethow写成:

private static void rethrow(){
    try{
        if (test) 
            throwIt(); // 15
        else 
            throwIt(); // 17
    } catch (Exception ex) {
        Console.WriteLine(ex.ToString());
        throw; // 20
    }
}

产量

System.Exception: Test
  at ConsoleApplication1.Program.throwIt() in Program.cs:line 10
  at ConsoleApplication1.Program.rethrow() in Program.cs:line 20
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 26

对throwIt()的哪个调用引发了异常?堆栈说第20行也是第15行或第17行?

解决方案与前一个相同:将原因包装在一个新的异常中,产生:

System.Exception: rethrow ---> System.Exception: Test
  at ConsoleApplication1.Program.throwIt() in Program.cs:line 10
  at ConsoleApplication1.Program.rethrow(Boolean test) in Program.cs:line 17
  --- End of inner exception stack trace ---
  at ConsoleApplication1.Program.rethrow(Boolean test) in Program.cs:line 20
  at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 26

我对这一切的简单结论是永远不要使用“throw;”而是要总是用指定的原因重新抛出一个新的异常。