1. 概述

  本章包括.net4.5中异常处理相关的部分。

2. 主要内容

  2.1 处理异常

    ① try、cahtch、finally 机制,无需多言。

    ② 使用 Environment.FailFast 方法,可以立即终止程序,并写入系统事件日志。会绕过finally的执行。

public static void Main()
{
string s = Console.ReadLine();
try
{
int i = int.Parse(s);
if (i == )
Environment.FailFast("Special number entered..");
}
finally
{
Console.WriteLine("Program complete..");
}
}

      ③ 如果finally中的代码发生异常,将会抛出到外层的处理程序,并且会丢失初始的异常信息。

  2.2 抛出异常

    ① 不要试图重用异常对象,特别是在多线程环境中。因为异常的堆栈信息可能会被其他线程更改。

    ② C#5 中新加了ExceptionDispatchInfo.Throw, 它会保存原始的异常信息并向上抛出。主要用于在线程间传递异常信息。

ExceptionDispatchInfo possibleException = null;

try
{
string s = Console.ReadLine();
int .Parse(s);
}
catch(FormatException ex)
{
possibleException = ExceptionDispatchInfo.Capture(ex);
} if (possibleException != null)
possibleException.Throw();

    ③ 异常处理会影响程序的流程和性能,降低可读性。

    ④ 一些只能被Runtime抛出的异常: AirthmaticException, ArrayTypeMisMatchException, DivideByZeroException,

      IndexOutofRangeException, InvalidCastException, NullReferenceException, OutOfMemoryException,

      OverFlowException, StackOverflowException, TypeInitializationException.

  2.3 创建自定义异常

    自定义异常应该按照规则,以Exception结尾。应该添加序列化属性。不要从System.ApplicationException继承。

[Serializable]
public class OrderProcessingException : Exception, ISerializable
{
public OrderProcessingException(int orderId)
{
OrderId = orderId;
this.HelpLink = "http://www.mydomain.com/infoExps";
}
public OrderProcessingException(int orderId, string msg)
: base (msg)
{
OrderId = orderId;
this.HelpLink = "http://www.mydomain.com/infoExps";
}
public OrderProcessingException(int orderId, string msg,
Exception innerExp) : base (msg, innerExp)
{
OrderId = orderId;
this.HelpLink = "http://www.mydomain.com/infoExps";
} protected OrderProcessingException(SerializationInfo info,
StreamingContext context)
{
OrderId = (int) info.GetValue("OrderId", typeof(int));
} public int OrderId {get; private set;} public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("OrderId", OrderId, typeof(int));
}
}

3. 总结

  ① 在.net中,应该用提供的异常处理机制来处理异常。

  ② 应该使用一个try块配合一个或多个catch块来处理不同类型的异常。

  ③ 自定义异常,只有在确定代码使用者将会有逻辑的处理它的时候才应该使用。否则就应该用内建的异常类型。

最新文章

  1. 学习ASP.NET 5和MVC6
  2. java string.format()
  3. Windows程序消息机制浅析
  4. ArcGIS 10 影像去黑边
  5. 怎样在delphi中实现控件和窗体的拖拽
  6. table 表格隔行换色实现
  7. 【Machine Learning in Action --5】逻辑回归(LogisticRegression)
  8. WinSCP 中普通用户以 root 身份登录 Linux
  9. 如何复制Google浏览器的控制台内容
  10. 系统及DB参数引发的“灾难”
  11. SHA-256算法和区块链原理初探
  12. dubbo的服务发现和注册如何实现
  13. 11.8 开课二个月零四天 (Jquery)
  14. RTC(x86)
  15. pre 强制换行
  16. 字符编码(ASCII,Unicode和UTF-8) 和 大小端(zz)
  17. 重装windows导致grub损坏
  18. Nginx配置参数说明
  19. Uva12230Crossing Rivers 数学
  20. “帮你APP”团队冲刺5

热门文章

  1. MSDN(电驴)
  2. Prim算法POJ1258
  3. CentOS 6.5 Maven 编译 Apache Tez 0.8.3 踩坑/报错解决记录
  4. 过河-状压DP
  5. openGL 提升渲染性能 之 顶点数组 VBO IBO VAO
  6. WebApi简单使用
  7. c++回调
  8. 如何用手机维护Mysql数据库
  9. leetcode022. Generate Parentheses
  10. 【Python】django权限管理