在日常C#的编码过程中,我们常常会使用try...catch...来抓住代码异常,并且在异常的时候打印log, 如下

             try
{ }
catch (Exception e)
{
//输出Log信息等
throw;
}

而对于catch括号里的(Exception e),需要输出哪些感兴趣的信息呢?我在看别人代码的过程中,发现
有的人会打印出e.Source,有的会打印出e.Message,有的会打印出e.StackTrace,而有的则直接打印

e.ToString()。这几种打印方式哪种是比较合适的呢?为此楼主特地查看了MSDN对Exception各属性的解

释,如下:

属性:

InnerException 获取导致当前异常的 Exception 实例。

Message 获取描述当前异常的消息。

Source 获取或设置导致错误的应用程序或对象的名称。

StackTrace 获取调用堆栈上的即时框架字符串表示形式。

TargetSite 获取引发当前异常的方法。

方法:

ToString() 创建并返回当前异常的字符串表示形式。(覆盖 Object.ToString()。)

而具体结果是怎么样的呢,楼主特地写了个简单的程序来测试。

 private void TestException()
{
try
{
StackPanel P = null;
Console.WriteLine("{0}",P.Width);
}
catch (System.Exception ex)
{
Console.WriteLine(string.Format("ex.ToString(). {0}\n", ex.ToString())); Console.WriteLine(string.Format("ex.Message. {0} \n", ex.Message)); Console.WriteLine(string.Format("ex.StackTrace. {0} \n", ex.StackTrace)); Console.WriteLine(string.Format("ex.Source. {0}.\n", ex.Source)); Console.WriteLine(string.Format("ex.TargetSite. {0}.\n", ex.TargetSite));
}
}

输出结果如图:

由上可知,e.ToString()打印出来的信息是最全的,包括了e.Message,e.StackTrace,e.Source等信息。

查看了一下MSDN上对于e.ToString()的实现,解释如下:

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. If there is no error message or if it is an empty string

(""), then no error message is returned. The name of the inner exception and the stack trace

are returned only if they are not null.

解释为:默认实现 ToString 获取引发当前异常、 消息、 调用的结果的类名称 ToString 对内部异常和

调用的结果 Environment.StackTrace。 如果任何这些成员为 null,其值不包括在返回的字符串。如果没有任何错误消息,或者为空字符串 (""),则不返回任何错误消息。 仅当它们不是返回的内部异常

和堆栈跟踪名称 null。

综上所述,一般输出log的时候,只需调用ex.ToString()方法就可以得到完整的信息,包括e.Message,e.StackTrace,e.Source等信息,不需要另外调用e.Message,e.StackTrace,e.Source等。

Exception.ToString()使用及其他方法比较的更多相关文章

  1. Exception 的 toString() 方法和 getMessage() 方法的区别

    Exception 的 toString() 方法和 getMessage() 方法的区别: 在开发的过程中打印错误日志时尽量使用e.toString() 方法, 因为当错误为空指针时 e.getMe ...

  2. LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。

    var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new { MatNR = o.MatNR, ...

  3. java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解

    public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...

  4. Object、String、数组的 toString() 方法和 equals() 方法及java.util.Arrays

    public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...

  5. Use Exception.ToString() instead of Exception.Message.

    Exception.Message contains only the message (doh) associated with the exception. Example: Object ref ...

  6. LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法

    一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...

  7. Java 异常Exception e中e的getMessage()和toString()以及 e.printStackTrace();方法的区别

    Exception e中e的getMessage()和toString()方法的区别: 示例代码1: public class TestInfo {     private static String ...

  8. 严重: Exception starting filter struts2解决方法!

    转自:http://blog.knowsky.com/260578.htm 问题出现: 严重: Exception starting filter struts2java.lang.ClassNotF ...

  9. Could not create the view: An unexpected exception was thrown的解决方法

    MyEclipse下面的server窗口突然不能正常显示了,而且还显示Could not create the view: An unexpected exception was thrown(无法创 ...

随机推荐

  1. virtualenv+pyenv管理python工作环境

    因为python2与3之间存在差异,所以日常工作中可能需要在2与3之间来回切换.在相同的python版本中,有可能有的项目需要用到django1.8,别的项目需要用到django1.9,所以如果可以在 ...

  2. Java框架spring 学习笔记(十二):aop实例操作

    使用aop需要在网上下载两个jar包: aopalliance.jar aspectjweaver.jar 为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将 ...

  3. His表(简化)

    门诊登记,门诊结算,门诊处方,住院登记,住院结算,住院处方,转诊登记,人员表,行政区划,登录日志,菜单,疾病,药品,诊疗,数据字典,机构,科室等

  4. Git简单生成生成公钥和私钥方法

    Git简单生成生成公钥和私钥方法 Git配置 Git安装完之后,需做最后一步配置.打开git bash,分别执行以下两句命令 git config --global user.name “用户名” g ...

  5. Hadoop环境搭建--Docker完全分布式部署Hadoop环境(菜鸟采坑吐血整理)

    系统:Centos 7,内核版本3.10 本文介绍如何从0利用Docker搭建Hadoop环境,制作的镜像文件已经分享,也可以直接使用制作好的镜像文件. 一.宿主机准备工作 0.宿主机(Centos7 ...

  6. Huawei BGP和OSPF双边界重分布(一)

    网络拓扑: PS:本例使用明细前缀列表双边界引入 S5700-LSW1 ================================================================ ...

  7. jdbc导致的问题

    遇到的问题:利用eclipse编写web project,Tomcat服务器成功开启,也通过build path导入了jdbc的路径,但是还是出现下面问题 连接数据库代码如下: 连接数据库语句没有问题 ...

  8. js实现接口隔离

    昨天公司培训了接口隔离,简单说一下 接口隔离:类间的依赖关系应该建立在最小的接口上.接口隔离原则将非常庞大.臃肿的接口拆分成更小具体的接口,这样客户讲会只需要知道他们感兴趣的方法. 接口隔离原则的目的 ...

  9. labellmg的使用

    ---恢复内容开始--- 在powershell环境下进入到labelImg解压后的文件,我这里是D:\labelImg-master\labelImg-master 执行命令: pyrcc5 -o ...

  10. redis常用服务安装部署

    常用服务安装部署   学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环境: centos7 + vmware + xshell 即将登场的是: mysql(m ...