我想抛出一个异常,但自定义消息并坚持栈跟踪。我已经通过各种线程。抛出异常,但坚持堆栈跟踪
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
}
当最后appraoch使用(与自定义异常构造调用基类的构造函数),输出对死亡的屏幕是一样的东西:
**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
的好处是一切都没有在屏幕上。但是,最重要的是,我希望我的例外显示“新消息”,而不是原始消息。
因此,调和我的问题:如何显示在死亡的屏幕上的原始堆栈跟踪,但与自定义错误消息?
这是一个Web服务返回的异常? – Oded
在我的例子中,它确实是一个web服务调用。然而,它是无关紧要的,因为异常可能是像DivisionByZero或sqlException之类的东西。想法是让用户知道原始堆栈跟踪,但是开发人员还可以自定义默认异常消息以获得更多帮助。 – helloworld
不,它并不重要。 Webservice框架对异常有自己的想法。 –