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块来处理不同类型的异常。

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

第五章 管理程序流(In .net4.5) 之 异常处理的更多相关文章

  1. 第三章 管理程序流(In .net4.5) 之 实现程序流

    1. 概述 本章内容包括 布尔表达式.流控制方式.集合遍历 以及 流跳转. 2. 主要内容 *由于该章内容比较基础,日常用的也很多,故对一些常用的基础内容不再赘述. 2.1 使用布尔表达式 熟悉下列比 ...

  2. 第一章 管理程序流(In .net4.5) 之 实现多线程和异步处理

    1. 概述 本章主要讲解.net4.5如何实现多线程和异步处理的相关内容. 2. 主要内容 2.1 理解线程 ① 使用Thread类   public static class Program   { ...

  3. 第四章 管理程序流(In .net4.5) 之 事件和回调

    1. 概述 本章讲解如何使用 委托.lambda表达式 和 匿名方法 来创建和使用事件. 2. 主要内容 2.1 理解委托 委托是一种用方法签名形式定义的类型.可以让它指向其他方法,可以通过它调用其他 ...

  4. 第二章 管理程序流(In .net4.5) 之 管理多线程

    1. 概述 本章包括同步资源以及取消长时间任务相关的内容. 2. 主要内容 2.1 同步资源 ① lock关键字实现.会阻塞程序,有可能会导致死锁. ② volatile关键字可以禁用编译优化,用于避 ...

  5. 精通Web Analytics 2.0 (7) 第五章:荣耀之钥:度量成功

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第五章:荣耀之钥:度量成功 我们的分析师常常得不到我们应得的喜欢,尊重和资金,因为我们没有充分地衡量一个黄金概念:成果.因为我们 ...

  6. 《APUE》第五章笔记

    第五章具体介绍了标准I/O库的各种细节,要是一一列出来,有费精力且可能列不全,故只讲平常多用到的.标准输入输出是由一大批函数组成的. 要记住,标准输入输出是有缓冲的,就是当缓冲区的数据满了的时候,才会 ...

  7. 第五章SignalR的实时高频通讯

    第五章SignalR的实时高频通讯 概述:本例子演示了如果创建一个对象与其他浏览器共享实时状态的应用程序.我们要创建的应用程序为“MoveShape”,该MoveShape页面会显示一个Html Di ...

  8. CSS3秘笈复习:十三章&十四章&十五章&十六章&十七章

    第十三章 1.在使用浮动时,源代码的顺序非常重要.浮动元素的HTML必须处在要包围它的元素的HTML之前. 2.清楚浮动: (1).在外围div的底部添加一个清除元素:clear属性可以防止元素包围浮 ...

  9. 《算法》第五章部分程序 part 8

    ▶ 书中第五章部分程序,包括在加上自己补充的代码,适用于基因序列的 2-Bit 压缩算法,行程长压缩算法,Huffman 压缩算法,LZW 压缩算法 ● 适用于基因序列的 2-Bit 压缩算法 pac ...

随机推荐

  1. python 实例属性之单,双下划线

    具体区别看下面例子 class A: def __init__(self,name='Andy'): self._name = name class B: def __init__(self,name ...

  2. Android——ScrollView

    1.activity_scrollview.xml <?xml version="1.0" encoding="utf-8"?><Scroll ...

  3. Android wifi状态三种广播

    public class NetworkConnectChangedReceiver extends BroadcastReceiver{      @Override      public voi ...

  4. Solr数据库连接之多表关联

    Solr环境配置好后,有很多时候我们需要把数据库里的数据添加到索引里,这时就需要配置跟数据库的连接,下面我们看配置的步骤. 1. 配置 solrconfig.xml  (在slor 主目录 core ...

  5. Gem5全系统模式下运行SPLASH-2 Benchmarks使用alpha ISA

    Steps to run the SPLASH-2 Benchmarks on M5 in full system mode using the alpha ISA. This Guide is ai ...

  6. oracle 清除当前用户的回收站

    --清除当前用户的回收站:purge recyclebin;  --删除表数据truncate table --查看当前用户回收站select * from user_recyclebin t; 

  7. java.lang.Exception: Socket bind failed: [730013] An attempt was made to acc

    在CMD命令行中启动运行startup.bat,启运程序总是闪退,查看日志发现如下错误: 26-Jan-2016 18:12:34.463 SEVERE [main] org.apache.coyot ...

  8. 华为OJ平台——输出最小的k个数

    输入n个整数,输出其中最小的k个. 详细描述: 接口说明 原型: bool GetMinK(unsignedint uiInputNum, int *pInputArray, unsignedint ...

  9. c#中如何不通过后台直接用js筛选gridview中的数据条件筛选查询?

    js: //条件筛选 var showstate = true; function imagechange() { if (showstate) { $('#_toggle').hide(500, f ...

  10. html5 搖一搖

    <script> // 首先在页面上要监听运动传感事件 function init(){ if (window.DeviceMotionEvent) { // 移动浏览器支持运动传感事件 ...