我想抛出一个异常,但是需要自定义消息并保留堆栈信息。我查阅了各种帖子。
catch (Exception ex)
{
throw; // Message is read only but stacktrace persist
throw ex; // Message is readonly and strack trace also blows .. worst!
throw new CustomException("My exception");// Message can be overridden but stacktrace lost
throw new CustomException("My message",ex);// same as above. However if this constructor in exception class calls same constructor of base class then .. see below
}
当使用最后一种方法(使用自定义异常构造函数调用基类构造函数)时,死亡屏幕上的输出如下:
**The remote server returned an error: (401) Unauthorized.**
[WebException: The remote server returned an error: (401) Unauthorized.]
original stack trace
[NewException: newMessage]
New Stack Trace
好的事情是一切都在屏幕上显示。但是,我希望在顶部显示我的异常,即“新消息”,而不是原始消息。
因此,我要解决的问题是:如何在死机屏幕上显示原始堆栈跟踪,但带有自定义错误消息?