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

1             try
2             {
3
4              }
5             catch (Exception e)
6             {
7                 //输出Log信息等
8                 throw;
9             }                    

而对于catch括号里的(Exception e),需要输出哪些感兴趣的信息呢?我在看别人代码的过程中,发现
有的人会打印出e.Source,有的会打印出e.Message,有的会打印出e.StackTrace,而有的则直接打印
e.ToString()。这几种打印方式哪种是比较合适的呢?为此楼主特地查看了MSDN对Exception各属性的解
释,如下:

属性:

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

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

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

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

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

方法:

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

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

 1 private void TestException()
 2      {
 3        try
 4        {
 5           StackPanel P = null;
 6           Console.WriteLine("{0}",P.Width);
 7        }
 8       catch (System.Exception ex)
 9        {
10           Console.WriteLine(string.Format("ex.ToString(). {0}\n", ex.ToString()));
11
12           Console.WriteLine(string.Format("ex.Message. {0} \n", ex.Message));
13
14         Console.WriteLine(string.Format("ex.StackTrace. {0} \n", ex.StackTrace));
15
16           Console.WriteLine(string.Format("ex.Source. {0}.\n", ex.Source));
17
18          Console.WriteLine(string.Format("ex.TargetSite. {0}.\n", ex.TargetSite));
19        }
20 }

输出结果如图:

由上可知,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等。

转载于:https://www.cnblogs.com/kushisei/p/6498473.html

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,Ma ...

  3. 华为手机刷屏老显示服务器出错,华为手机刷机出现update exception emmc is readonly解决方法...

    华为手机刷机出现update exception emmc is readonly解决方法 大家在给华为手机强制sd卡刷机失败时提示 updat exception EMMC is readonly ...

  4. threw ‘java.lang.NullPointerException‘ exception // toString()

    Method threw 'java.lang.NullPointerException' exception. Cannot evaluate org.json.JSONObject.toStrin ...

  5. java中Object和Objects类、toString方法和equals方法介绍

    Object类: Object类位于java.lang包中,lang包下的类无需导包就可以使用,object是Java语言中的根类,即所有类的父类.Object中描述的所有方法其子类都可以使用.在对象 ...

  6. 为什么要重写toString()方法和hashcode()方法

    一.toString(): 在Object类里面定义toString()方法的时候返回的对象的哈希code码,这个hashcode码不能简单明了的表示出对象的属性.所以要重写toString()方法. ...

  7. toString方法和String方法

    要把一个值转换成字符串有两个方法,toString   Sting toString()方法:数值,布尔值,对象和字符串值(没错,每个字符串都有一个toSrting()方法,该方法返回一个字符串副本) ...

  8. PHP Fatal error: Uncaught think\\exception\\ErrorException: error_log相关解决方法

    解决方法:切换到tp5目录然后使用chmod -R 777 tp5来解决,当然不太建议这样做,这不符合我们在线上的操作方式.一种比较推荐的做法是执行chmod -R daemon:daemon tp5 ...

  9. PHP Fatal error: Uncaught think\\exception\\ErrorException: error_log相关解决方法

    解决方法:切换到tp5目录然后使用chmod -R 777 tp5来解决,当然不太建议这样做,这不符合我们在线上的操作方式.一种比较推荐的做法是执行chmod -R daemon:daemon tp5 ...

最新文章

  1. [CF888G]Xor-MST
  2. 计算机一级多选题没有选分,计算机一级多选题
  3. 半圆阴影_圆中阴影部分面积求法的常用方法
  4. 数据管理(八)--CD程序
  5. STEAM教育风口正劲,如何培养STEAM思维?
  6. springboot md5加密_实在!基于Springboot和WebScoket,写了一个在线聊天小程序
  7. postfixadmin 导入数据库出错 ERROR 1062 (23000) at line 29
  8. jdk源码——HashMap
  9. 微信小程序之验证码短信倒计时
  10. 【定位问题】基于matlab三维chan算法求解室内定位问题【含Matlab源码 580期】
  11. Hadoop大数据实战权威指南
  12. brctl: command not found
  13. 情迁机器人Tim_情迁机器人插件-情迁机器人app下载V1.5.0安卓版-西西软件下载
  14. mbr分区磁盘如何转换成gpt分区磁盘?
  15. 英语单词12大前缀3大词性后缀
  16. 系列教程|Apache Doris ODBC 外表使用指南(一)
  17. RFID天线技术 – 低频和高频RFID天线设计
  18. 【男保姆式】教你打开第一个微信小程序
  19. 2.try(){}语法含义及用法
  20. RocketMQ之消息重试。

热门文章

  1. 机器人改变生活利弊英语作文_左手建筑右手餐饮,机器人如何改变大众生活? 圆梦人感言...
  2. hadoop2.9.2安装hadoop_pid_dir_Hadoop3.2 +Spark3.0全分布式安装
  3. grep 两个字符串_Linux运维工程师想要的12个grep指令
  4. html5多个图片位置_图表分析中的HTML5地图需求,这款BI轻松5步就给解决了
  5. 冯永昌:云计算与大数据时代的量化投资
  6. 折线图_R语言画折线图?
  7. allure 测试报告本地打开_Allure自动化测试报告我是这样用的
  8. 系统学习NLP(二十九)--BERT
  9. 系统学习机器学习之弱监督学习(三)--Adversarial Autoencoders
  10. Hive日期格式转换