我一直在玩Exceptions以了解我应该如何正确使用它们.到目前为止,我知道throw
保持原始堆栈跟踪; throw new CustomException(...)
通常用于想要添加有关发生的异常的更多信息或添加/更改消息,甚至更改异常本身的类型; 并且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
是一个自定义异常正确实施.
我期待控制台显示如下内容:
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
而不是抛出一个新的异常,我可以保留原始的堆栈跟踪,但如果我这样做,我将无法更改原始消息(这是本练习的全部内容) .
所以我的问题是,我该怎么做才能更改Exception消息并保持原始堆栈跟踪的方式?
编辑:由于异常不应该使用逻辑控制并且只捕获一次,保持原始堆栈跟踪并显示新消息的正确方法是将FourthCall包装在try/catch中(其中生成带有消息的新Exception) ),并且只在FirstCall中一次捕获它.
堆栈跟踪不会"丢失"它被推入InnerException,就像你告诉它的那样.在这种情况下,"外部"异常没有参与Inner异常的调用链 - 它是一个全新的异常,它起源于SecondCall,因此它是堆栈跟踪的开始.
是的,评论者是正确的.要控制您的消息传递,您不会通过尝试在Exception对象中设置消息来做到这一点 - 异常应由代码处理,消息用于用户.因此,您将记录消息,将其显示给用户,类似于此.