C#相当于Java的Exception.printStackTrace()? [英] C# equivalent to Java's Exception.printStackTrace()?

查看:165
本文介绍了C#相当于Java的Exception.printStackTrace()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有与Java的 Exception.printStackTrace()的C#等价的方法,或者我必须自己写点东西,通过InnerExceptions的方式吗?

Is there a C# equivalent method to Java's Exception.printStackTrace() or do I have to write something myself, working my way through the InnerExceptions?

推荐答案

尝试这样:

Console.WriteLine(ex.ToString());

http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx


ToString的默认实现获取抛出当前异常的类的名称,消息,内部异常调用ToString的结果以及调用结果Environment.StackTrace。如果这些成员中的任何一个为null,则其值不包括在返回的字符串中。

The default implementation of ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is null, its value is not included in the returned string.

请注意,在上述代码中, ToString 不需要,因为有一个重载,需要 System.Object 并调用 ToString 直接。

Note that in the above code the call to ToString isn't required as there's an overload that takes System.Object and calls ToString directly.

这篇关于C#相当于Java的Exception.printStackTrace()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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