加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 抛出异常但保持堆栈跟踪

发布时间:2020-12-15 04:04:34 所属栏目:百科 来源:网络整理
导读:我想抛出一个异常,但也有自定义消息和persist stacktrace.我经历了各种各样的线索. catch (Exception ex){throw; // Message is read only but stacktrace persistthrow ex; // Message is readonly and strack trace also blows .. worst!throw new CustomE
我想抛出一个异常,但也有自定义消息和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

好东西是屏幕上的一切都在那里.但是,最重要的是我希望我的例外显示“新消息”而不是原始消息.

因此调和我的问题:如何在死亡屏幕上显示原始堆栈跟踪但是有自定义错误消息?

解决方法

throw new CustomException("My message",ex);// same as above (... stacktrace lost)

你在评论中得出的结论在最后一篇文章中是错误的.堆栈跟踪保留在内部异常中.标准报告(包括Exception.ToString())将报告完整的堆栈跟踪.当你获得正确的构造函数时,这就是你所看到的. (始终打电话给正确的基地!).

但我不认识[WebException].在WCF中你需要

<serviceDebug includeExceptionDetailInFaults="true"/>

我猜您的Web环境具有类似的功能,可以抑制客户端的错误信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读