A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 逝者轨迹 中级黑马   /  2013-5-11 11:08  /  892 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

2.4.  如何:使用 Finally
异常发生时,执行将终止,并且控制交给最近的异常处理程序。这通常意味着不执行希望总是调用的代码行。有些资源清理(如关闭文件)必须总是执行,即使有异常发生。为实现这一点,可以使用 Finally 块。Finally 块总是执行,不论是否有异常发生。
下面的代码示例使用 try/catch 块捕获 ArgumentOutOfRangeException。Main 方法创建两个数组并试图将一个数组复制到另一个数组。该操作生成 ArgumentOutOfRangeException,同时错误被写入控制台。Finally 块执行,不论复制操作的结果如何。
using System;
class ArgumentOutOfRangeExample
{
         static public void Main()
  {
  int[] array1={0,0};
  int[] array2={0,0};
     try
     {
     Array.Copy(array1,array2,-1);
     }
     catch (ArgumentOutOfRangeException e)
     {
     Console.WriteLine("Error: {0}",e);
     }
     finally
     {
     Console.WriteLine("This statement is always executed.");
     }
  }
}
3.   异常设计准则
3.1.  异常引发
不要返回错误代码。异常是报告框架中的错误的主要手段。
尽可能不对正常控制流使用异常。除了系统故障及可能导致争用状态的操作之外,框架设计人员还应设计一些 API 以便用户可以编写不引发异常的代码。例如,可以提供一种在调用成员之前检查前提条件的方法,以便用户可以编写不引发异常的代码。
不要包含可以根据某一选项引发或不引发异常的公共成员。
不要包含将异常作为返回值或输出参数返回的公共成员。
考虑使用异常生成器方法。从不同的位置引发同一异常会经常发生。为了避免代码膨胀,请使用帮助器方法创建异常并初始化其属性。
避免从 finally 块中显式引发异常。可以接受因调用引发异常的方法而隐式引发的异常。
3.2.  异常处理
不要通过在框架代码中捕捉非特定异常(如 System.Exception、System.SystemException 等)来处理错误。
避免通过在应用程序代码中捕捉非特定异常(如 System.Exception、System.SystemException 等)来处理错误。某些情况下,可以在应用程序中处理错误,但这种情况极。
如果捕捉异常是为了传输异常,则不要排除任何特殊异常。
如果了解特定异常在给定上下文中引发的条件,请考虑捕捉这些异常。
不要过多使用 catch。通常应允许异常在调用堆栈中往上传播。
使用 try-finally 并避免将 try-catch 用于清理代码。在书写规范的异常代码中,try-finally 远比 try-catch 更为常用。
捕捉并再次引发异常时,首选使用空引发。这是保留异常调用堆栈的最佳方式。
不要使用无参数 catch 块来处理不符合 CLS 的异常(不是从 System.Exception 派生的异常)。支持不是从 Exception 派生的异常的语言可以处理这些不符合 CLS 的异常。
4.   两种设计模式
4.1.  Tester-Doer 模式
Doer 部分
public class Doer
{
    public static void ProcessMessage(string message)
    {
        if (message == null)
        {
            throw new ArgumentNullException("message");
        }
    }
}
Tester部分
public class Tester
{
    public static void TesterDoer(ICollection<string> messages)
    {
        foreach (string message in messages)
        {
            if (message != null)
            {
                Doer.ProcessMessage(message);
            }
        }
    }
}
4.2.  TryParse 模式
TryParse 方法类似于 Parse 方法,不同之处在于 TryParse 方法在转换失败时不引发异常。
Parse方法
public void Do()
{
    string s = “a”;
    double d;
    try
    {
        d = Double.Parse(s);
    }
    catch (Exception ex)
    {
        d = 0;
    }
}
TryParse方法
public void TryDo()
{
    string s = "a";
    double d;
    if (double.TryParse(s, out d) == false)
    {
        d = 0;
    }
}
5.   微软企业库异常处理模块
5.1.  创建自定义异常包装类
public class BusinessLayerException : ApplicationException
{
     public BusinessLayerException() : base()
     {
     }
     public BusinessLayerException(string message) : base(message)
     {
     }
   
     public BusinessLayerException(string message, Exception exception) :
         base(message, exception)
     {
     }
     protected BusinessLayerException(SerializationInfo info, StreamingContext context) :
         base(info, context)
     {
     }
}
5.2.  配置异常处理
<add name="Wrap Policy">
        <exceptionTypes>
          <add type="System.Data.DBConcurrencyException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException" name="DBConcurrencyException">
            <exceptionHandlers>
              <add exceptionMessage="Wrapped Exception: A recoverable error occurred while attempting to access the database."
                exceptionMessageResourceType="" wrapExceptionType="ExceptionHandlingQuickStart.BusinessLayer.BusinessLayerException, ExceptionHandlingQuickStart.BusinessLayer"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                name="Wrap Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
5.3.  编写代码
public bool ProcessWithWrap()
{
    try
    {
         this.ProcessB();
    }
    catch(Exception ex)
    {
         // Quick Start is configured so that the Wrap Policy will
         // log the exception and then recommend a rethrow.
         bool rethrow = ExceptionPolicy.HandleException(ex, "Wrap Policy");
         if (rethrow)
         {
             throw;            
         }            
    }
    return true;
}

评分

参与人数 1技术分 +1 收起 理由
HM汪磊 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马