Exception.ToString()使用及其他方法比较
在日常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()使用及其他方法比较的更多相关文章
- Exception 的 toString() 方法和 getMessage() 方法的区别
Exception 的 toString() 方法和 getMessage() 方法的区别: 在开发的过程中打印错误日志时尽量使用e.toString() 方法, 因为当错误为空指针时 e.getMe ...
- LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new { MatNR = o.MatNR, ...
- 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 ...
- 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 ...
- Use Exception.ToString() instead of Exception.Message.
Exception.Message contains only the message (doh) associated with the exception. Example: Object ref ...
- LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法
一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...
- Java 异常Exception e中e的getMessage()和toString()以及 e.printStackTrace();方法的区别
Exception e中e的getMessage()和toString()方法的区别: 示例代码1: public class TestInfo { private static String ...
- 严重: Exception starting filter struts2解决方法!
转自:http://blog.knowsky.com/260578.htm 问题出现: 严重: Exception starting filter struts2java.lang.ClassNotF ...
- Could not create the view: An unexpected exception was thrown的解决方法
MyEclipse下面的server窗口突然不能正常显示了,而且还显示Could not create the view: An unexpected exception was thrown(无法创 ...
随机推荐
- 解决mysql设置时区时的错误Unknown or incorrect time zone: 'Asia/Shanghai'
Mysql默认时区格式是'+8:00'的格式,这个时区可以在my.ini中[mysqld]节点下设置 default-time-zone = '+8:00' 默认这个设置是没有的 但是mysql不支持 ...
- mac相关功能
打开和关闭索引功能 打开:sudo mdutil -a -i on 关闭:sudo mdutil -a -i off 关闭后则无法搜
- 合并回文子串(区间dp)
链接:https://ac.nowcoder.com/acm/problem/13230来源:牛客网 题目描述 输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如" ...
- springmvc的面试知识点总结
新的一年,开启新的篇章,欧气满满,迎接未来. 前几天回顾了spring相关的知识点,现在再来回顾下springmvc相关的知识点做一下总结. 问题总结 之前面试问题总结的那篇文章中,与springmv ...
- qt quick-初始学习概念
Qt Quick简介: Qt Quik 是一种高级用户界面技术,使用它可以轻松地创建供移动和嵌入式设备使用到动态触摸式界面和轻量级应用程序:Qt Quick主要由三部份组成: 改进的Qt Creato ...
- MySQL解压包的安装教程
一.下载MySQL解压包 解压过的文件夹里面是没有 data 文件夹的. 二.创建文件 1.在根目录下创建 my.ini文件 内容如下: [mysqld] # 设置mysql的安装目录 basedir ...
- 《修炼之道:.NET开发要点精讲》读书笔记(三)
后几章的习题 1.异步调用开始后,什么时候才能使用异步执行的结果? A:最好在EndInvoke()方法返回之后才能使用异步执行的结果,其它时候不能保证异步调用已完成. 2.委托的异步调用开始后(即调 ...
- 五、Pyqt5事件、信号和槽
PyQt中提供了两种针对事件处理的机制:一种是事件,另一种则是信号和槽. 一.事件 事件处理在PyQt中是比较底层的,常用的事件有键盘事件.鼠标事件.拖放事件.滚轮事件.定时事件.焦点事件.进入和离开 ...
- 二、PyQt5基本功能和操作入门
在这里,我将根据自己的学习历程从初级到高级介绍pyqt5.因为是学到哪里就写道哪里,所以内容排版比较随意.有两点问题需要先说明: 1.虽然界面的设计可以借助qt designer进行拖拽创建,并且可以 ...
- PNP的学习-P3P
PNP方法是为了解决在当前两帧图像中,已知前一帧图像上的3dLandmark点和当前帧的2d特征点,求取当前帧的pose. PNP主要有P3P.EPNP.UPNP.DLT.MRE(LS Iterati ...