c# - 多次抛出异常会丢失其原始堆栈跟踪

标签 c#

我一直在研究异常,以了解更多关于如何正确使用它们的信息。到目前为止,我知道 throw 保留了原始堆栈跟踪; throw new CustomException(...) 通常在想要添加有关发生的异常的更多信息或添加/更改消息,甚至更改 Exception 本身的类型时使用;永远不要使用 throw ex,除非我想丢失原始堆栈跟踪。

所以我编写了一个小程序,在向原始消息添加内容的同时,我可以多次捕获并重新抛出异常。

public class Sample
{
    static void Main(string[] args)
    {
        new Tester().FirstCall();
    }
}

public class Tester
{
    public void FirstCall()
    {
        try
        {
            SecondCall();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.StackTrace);
            Console.WriteLine(e.Message);
        }
    }

    public void SecondCall()
    {
        try
        {
            ThirdCall();
        }
        catch (GoodException ex)
        {
            throw new Exception(ex.Message, ex);
        }
    }

    public void ThirdCall()
    {
        try
        {
            FourthCall();
        }
        catch (ArithmeticException ae)
        {
            throw new GoodException("Arithmetic mistake: " + ae.Message, ae);
        }
    }

    public void FourthCall()
    {
        int d = 0;
        int x = 10 / d;
    }
}

GoodException 是自定义异常 implemented correctly .

我希望控制台显示如下内容:

   at PlayingWithExceptions.Tester.FourthCall() in d:\Projects\PlayingWithExceptions\PlayingWithExceptions\Trying.cs:line 67
   at PlayingWithExceptions.Tester.ThirdCall() in d:\Projects\PlayingWithExceptions\PlayingWithExceptions\Trying.cs:line 59
   at PlayingWithExceptions.Tester.SecondCall() in d:\Projects\PlayingWithExceptions\PlayingWithExceptions\Trying.cs:line 41
   at PlayingWithExceptions.Tester.FirstCall() in d:\Projects\PlayingWithExceptions\PlayingWithExceptions\Trying.cs:line 25
Arithmetic mistake: Attempted to divide by zero.

但是我得到的是:

   at PlayingWithExceptions.Tester.SecondCall() in d:\Projects\PlayingWithExceptions\PlayingWithExceptions\Trying.cs:line 41
   at PlayingWithExceptions.Tester.FirstCall() in d:\Projects\PlayingWithExceptions\PlayingWithExceptions\Trying.cs:line 25
Arithmetic mistake: Attempted to divide by zero.

出于某种原因,它只进行到第二次调用。即使我将捕获的异常作为 InnerException 传递,堆栈跟踪仍然丢失。我知道如果我只是写 throw 而不是抛出新的异常,我可以保留原始堆栈跟踪,但如果我这样做,我将无法更改原始消息(是本练习的重点)。

所以我的问题是,我该怎么做才能更改异常消息并在整个过程中保持原始堆栈跟踪?

编辑:由于不应使用逻辑控制异常并且只能捕获一次,因此保留原始堆栈跟踪并显示新消息的正确方法是将 FourthCall 包装在 try/catch 中(生成新异常及其消息的地方),并在 FirstCall 中一直捕获它一次。

最佳答案

堆栈跟踪并没有“丢失”,它被插入了 InnerException,就像您告诉它的那样。在这种情况下,“外部”异常没有参与内部异常的调用链 - 它是一个全新的异常,起源于 SecondCall,因此这是其堆栈跟踪的开始。

是的,评论者是正确的。要控制您的消息传递,您不会通过尝试在 Exception 对象中设置消息来做到这一点——异常应该由代码处理,消息是给用户的。因此,您将记录消息,将其显示给用户,诸如此类。

关于c# - 多次抛出异常会丢失其原始堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923505/

相关文章:

c# - 如何检查一个对象是否*完全*是一个类,而不是派生类?

c# - 带隧道到 MySQL 的 EF (C#) - 优化数据库调用

c# - 列出tfs中文件夹的所有内容

c# - 你能为 asp.net-mvc 推荐替代的 FileUpload 控件吗?

Int32 文字到 float 的 C# 性能损失

c# - 如何在 C++/CLI 中包装 C++ 接口(interface)(抽象类)?

c# - Web APi OData V4 问题 "The entity ' ' 没有定义键

c# - 将包含十六进制值的字节数组转换为十进制值

c# - XNA 如何在其中心旋转 3D 立方体?

c# - 以代码为参数的函数