我一直在研究异常,以了解更多关于如何正确使用它们的信息。到目前为止,我知道 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/