我想抛出异常,但也带有自定义消息和持久堆栈跟踪。我已经经历了各种线程。
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
好消息是屏幕上有一切。但是,最重要的是我希望显示我的异常,即“新消息”而不是原始消息。
因此,协调我的问题:如何在死亡屏幕上显示原始堆栈跟踪但带有自定义错误消息?
throw new CustomException("My message",ex);// same as above (... stacktrace lost)
您在评论中的结论在最后一项上是错误的。堆栈跟踪保存在内部异常中。标准报告(包括Exception.ToString()
)将报告完整的堆栈跟踪。这就是当你正确使用构造函数时你所看到的。 (始终调用正确的基本 ctor!)。
但我不认识[WebException]
。在WCF中你需要
<serviceDebug includeExceptionDetailInFaults="true"/>
我猜想您的 Web 环境具有类似的功能,可以抑制向客户端发送错误信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)