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(无法创 ...
随机推荐
- Window上编译最新版libCef(Branch 2704)(转载)
转自http://blog.csdn.net/mfcing/article/details/52066579 1.开发环境搭建 VS2010及以上版本,CMake 2.8.12.1及以上版本.我安 ...
- Gulp教程之:Gulp能做什么,前端装逼为何要用它
我们先说说 平时web开发遇到的一些场景 和 苦恼无奈的情况: JavaScript和CSS的版本问题 我们都知道 JavaScript和CSS属于静态文件,如果地址不变,浏览器会缓存这些文件,那就意 ...
- Python·——进程1
1.进程背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序(的一个抽象). 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统 ...
- 洛谷P4051 [JSOI2007]字符加密 后缀数组
题目链接:https://www.luogu.org/problemnew/show/P4051 思路:我们联想求后缀数组sa的过程,发现我们在求y数组的时候(第二关键字,下标为第二关键字的排位,值为 ...
- 跳转Activity时清除当前Activity
void GotoMainActivity(){ Intent intent = new Intent(ProductionInformationActivity.this, MainActivity ...
- HDU - 1695 GDU
莫比乌斯反演基础. 用rep 去掉重复的对数,rep一定是奇数( 因为有(1,1 ) ) #include <bits/stdc++.h> using namespace std; #de ...
- tomcat启动慢
securerandom.source=file:/dev/./urandom 搜索 /""
- WEB-INF目录下的jsp怎么引用外部文件:js,css等
在项目中,为了安全.我们通常会将jsp文件放在WEB-INF下面,对于放在WEB-INF下面的js或是css等资源文件.我们通常可以通过相对路径来引用,而如果是放在WEB-INF之外的js 或是 cs ...
- python词频统计及其效能分析
1) 博客开头给出自己的基本信息,格式建议如下: 学号2017****7128 姓名:肖文秀 词频统计及其效能分析仓库:https://gitee.com/aichenxi/word_frequenc ...
- .gitinore配置失效问题
问题:在.gitinore中配置忽略项,配置失效 原因:新增加忽略项已经提交过,在暂存区或分支上被版本控制 解决:删除暂存区或分支上的文件(本地需要使用, 只是不希望这个文件被版本控制), 可以使用 ...