不止一次抛出exception会丢失其原始堆栈跟踪

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

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

 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是正确实现的自定义exception。

我期待控制台显示如下内容:

  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. 

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

所以我的问题是,我该怎么做才能更改Exception消息并保持原始堆栈跟踪的全部方式?

编辑 :由于exception不应该使用逻辑控制并且只捕获一次,保持原始堆栈跟踪并显示新消息的正确方法是将FourthCall包装在try / catch中(其中生成带有消息的新Exception) ),并且只在FirstCall中一次捕获它。

堆栈跟踪不会“丢失”它被推入InnerException,就像你告诉它的那样。 在这种情况下,“外部”exception没有参与Innerexception的调用链 – 它是一个全新的exception,它起源于SecondCall,因此它是堆栈跟踪的开始。

是的,评论者是正确的。 要控制您的消息传递,您不会通过尝试在Exception对象中设置消息来执行此操作 – exception应由代码处理,消息适用于用户。 因此,您将记录消息,将其显示给用户,类似于此。

不知道它是否仍然适合你。 只需使用关键字“ throw ”而不附加exception,那么跟踪不会丢失,原始exception将被抛出。 不是内心的。