抛出异常,但仍然存在堆栈跟踪 [英] Throw exception but persist stack trace

查看:293
本文介绍了抛出异常,但仍然存在堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想抛出一个异常,而是使用自定义消息,并持续堆栈跟踪了。 。我已经通过不同的线程了。



 赶上(异常前)
{
扔; //信息是只读的,但堆栈跟踪持续
罚球前; //信息是只读的并且STRACK痕迹也吹..最差的!
抛出新CustomException(我的异常); //消息可以被覆盖,但堆栈跟踪丢失
抛出新CustomException(我的信息,前); //同上。但是,如果此构造异常类调用基类的构造函数相同..然后见下文
}

在过去的appraoch使用(使用自定义异常的构造函数调用基类构造函数),死亡的屏幕输出是一样的东西:

  **远程服务器返回错误:未经授权(401)** 

[引发WebException:远程服务器返回错误:(401)未经授权。
原有的堆栈跟踪

[NewException:NewMessage作为]
新的堆栈跟踪

好东西是一切都在那里在屏幕上。但是,在上面我希望我的异常显示,即新信息,而不是原始邮件



因此,调和,我的问题:我怎么能显示死亡原有的屏幕堆栈跟踪,但与自定义错误消息?


解决方案

 抛出新CustomException(我的消息恩); //同上(...堆栈跟踪中丢失)

您的结论评论是错误的最后一个。堆栈跟踪保存在内部异常。标准的报告(包括 Exception.ToString())将报告完整堆栈跟踪。这是当你得到正确的构造您所看到的东西。 (总是调用正确的构造函数的基础!)。



但我不承认 [引发WebException] 。在WCF中你需要

 < serviceDebug includeExceptionDetailInFaults =真/> 



我猜你的网络环境有抑制对客户端错误信息的类似的功能。


I want to throw an exception but with custom message and persist stacktrace too. I've gone through various threads.

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
}

When last appraoch is used (with custom exception constructor calling base class constructor), output on screen of death is something like:

**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

Good thing is everything is there on screen. But, on top I want my exception to display i.e. "new message" and not original message.

Hence reconciling my question: HOW can I display on screen of death the original stack trace but with a custom error Message?

解决方案

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

Your conclusion in the comments is wrong on the last one. The stacktrace is kept in the inner Exception. Standard reporting (including Exception.ToString()) will report the full stacktrace. This is what you are seeing when you get the constructor right. (Always call the correct base ctor!).

But I don't recognize [WebException] . In WCF you need

  <serviceDebug includeExceptionDetailInFaults="true"/>   

I guess your Web environment has a similar feature of suppressing error info towards the client.

这篇关于抛出异常,但仍然存在堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆