我想抛出一个异常,但也有自定义消息和persist stacktrace.我经历了各种各样的线索.
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
好东西是屏幕上的一切都在那里.但是,最重要的是我希望我的例外显示“新消息”而不是原始消息.