zoukankan      html  css  js  c++  java
  • c#自定义异常处理

    在另一篇文章里,我说了.NET异常处理机制中的自定义异常处理,今天我要说的是另外一种异常处理。

    在写处理程序异常的过程中,可能会遇到各种不同类型的异常,而已要抛出不同的人性化提示,如果统一抛出一样的提示,就不人性化了,

    我们一般的处理方法 是:

    1. public void Update()
    2. {
    3.        try
    4.        {   }
    5.        catch(SqlExcetion ex)
    6.        {
    7.               throw new excetion(ex);
    8.         }
    9.         catch(IOExcetion ex)
    10.          {
    11.                   throw new excetion(ex);
    12.           }
    13.           //其它异常…
    14.           catch (Exception ex)
    15.             {
    16.                 throw new Exceptioin(ex);
    17.             }
    18. }

     
     
     
     
     
               或者采用异常嵌套(异常里嵌套异常,这里就不上代码 了)
     
    但是这种方法有一个问题,如果有多个地方要这样处理,那我们真的成了代码工人了。所以,用一个类来管理它吧。
    在这里我们叫它,异常处理类。
    1.   /// <summary>
    2.     /// 全局异常处理类
    3.  ///作者:数据库之家(http://www.uol123.com)
    4.     /// </summary>
    5.     [Serializable]
    6.     public class ExceptioinManage :Exception
    7.     {
    8.         /// <summary>
    9.         /// 默认构造函数
    10.         /// </summary> 
    11.         public ExceptioinManage() { }
    12.  
    13.         public ExceptioinManage(string message)
    14.             : base(message) 
    15.         {
    16.             throw new Exception(message);
    17.         }
    18.  
    19.         public ExceptioinManage(string message, Exception inner)
    20.             : base(message, inner) { }
    21.  
    22.         public ExceptioinManage(System.Runtime.Serialization.SerializationInfo info,
    23.             System.Runtime.Serialization.StreamingContext context)
    24.             : base(info, context) { }
    25.  
    26.         public ExceptioinManage(Exception ex)         
    27.         {
    28.             if (ex.GetType() == typeof(System.Data.SqlClient.SqlException))
    29.             {
    30.                 //日志
    31.                 throw new Exception(“连接已断开,请检查网络的联通性!”);
    32.             
    33.        if (ex.GetType() == typeof(System.IO.IOExcetion))
            {
                  //日志
                 throw new Exception("无法读取文件!");
            }
            //其它异常
    34.             //日志(http://www.uol123.com)

    35.             throw ex;//默认异常
    36.         }
    37.     }
    这样,我们只需要把我们的   throw new Excetion(ex)   改为  throw new ExceptioinManage(ex) 就可以抛出人性化的异常了!
     
    参考资料:

    -------------------------------------------------------------------------------------------------------------------------------------------------
    数据库优化
    数据库教程
    数据库实战经验分享博客

    百度云下载

    评测


  • 相关阅读:
    windows的80端口被占用时的处理方法
    Ansible自动化运维工具安装与使用实例
    Tomcat的测试网页换成自己项目首页
    LeetCode 219. Contains Duplicate II
    LeetCode Contest 177
    LeetCode 217. Contains Duplicate
    LeetCode 216. Combination Sum III(DFS)
    LeetCode 215. Kth Largest Element in an Array(排序)
    Contest 176 LeetCode 1354. Construct Target Array With Multiple Sums(优先队列,递推)
    Contest 176
  • 原文地址:https://www.cnblogs.com/longle/p/2269100.html
Copyright © 2011-2022 走看看