zoukankan      html  css  js  c++  java
  • 扩展方法 --- 异常处理

    扩展方法: 异常处理。

    通常我们写异常经常在function内容添加Try catch..  在方法过多的时候这么写确实不是什么好办法。

    介绍一下我的扩展处理。 可能大家都是这么用的。会用的路过就剋以了。 不会的可以学习一下。

    扩展代码如下 :

    public static class ExceptionHelper
        {
            /// <summary>
            /// Exes the try asynchronous.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="fun">The fun.</param>
            /// <param name="errorMessage">The error message.</param>
            /// <returns></returns>
            /// <exception cref="System.Exception"></exception>
            public static async Task<T> ExTryAsync<T>(Func<Task<T>> fun, string errorMessage)
            {
                try
                {
                    return fun().Result;
                }
                catch (Exception ex)
                {
                    throw new Exception(errorMessage + "---->" + ex.Message);
                }
            }
    
            /// <summary>
            /// Exes the try.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="fun">The fun.</param>
            /// <param name="errorMessage">The error message.</param>
            /// <returns></returns>
            /// <exception cref="System.Exception"></exception>
            public static T ExTry<T>(Func<T> fun, string errorMessage)
            {
                try
                {
                    return fun();
                }
                catch (Exception ex)
                {
                    throw new Exception(errorMessage + "---->" + ex.Message);
                }
            }
    
            /// <summary>
            /// Exes the try.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="fun">The fun.</param>
            /// <param name="defaultValue">The default value.</param>
            /// <returns></returns>
            public static T ExTry<T>(Func<T> fun, T defaultValue)
            {
                try
                {
                    return fun();
                }
                catch (Exception ex)
                {
                    LogWriter.Error(ex);
                    return defaultValue;
                }
            }
        }

    使用方法:

        public string GetValue()
        {
            LogHelperExtensions.Info("Object -> GetValue ");
            var baseValue = new BaseValue { State = false, Value = null, ErrDes = "系统服务异常!" };
            return ExceptionHelper.ExTry(() =>
            {
                //自己的业务逻辑
                return baseValue;
            }, baseValue);
        }

    可以看到 我们通过异常扩展我们可以将每一个方法体内容处理日志信息。更好的维护了项目的异常管理。

    服务级别产生了bug我们也可以直接通过日志查询。

    多说句 微软的 Async 很不错!

  • 相关阅读:
    hadoop大数据平台架构之DKhadoop详解
    越狱设备看不到系统文件夹
    iOS开发 调试 网络限速
    关于OCMock的一些事儿
    Cannot proceed with delivery: an existing transporter instance is currently uploading this package
    windows安装nose
    IE6支持png半透明图片
    ie下web常见错误和差异及解决方案
    js,php下 css颜色加深、减淡,css颜色递进的方法
    html+css div百分百占满正行的两种方法
  • 原文地址:https://www.cnblogs.com/liuyunsheng/p/4286634.html
Copyright © 2011-2022 走看看