执行异常处理方法

应用场景:

     网站代码中,网络延迟,数据处理出错。

     指定时间点,任务执行中出现异常错误。    

     ...

异常处理通用方法:         

   /// <summary>
        /// 执行异常处理方法
        /// </summary>
        /// <typeparam name="P">参数类型</typeparam>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="Func">方法名</param>
        /// <param name="param">传入参数值</param>
        /// <returns>返回值</returns>
        static T Exec<P, T>(Func<P, T> Func, P param)
        {
            T t = default(T);
            bool isTrue = true;
            int count = 0;
            while (isTrue)
            {
                if (count <= 3)
                {
                    try
                    {
                        t = Func(param);
                        isTrue = false;
                    }
                    catch (Exception ex)
                    {
                        //Thread.Sleep(count * 3000);
                        isTrue = true;
                    }
                }
                else
                {
                    isTrue = false;
                }
                count++;
            }
            return t;
        }

示例方法:    

        static string GetInt(int param)
        {
            return param.ToString();
        }

 

执行:
  

string str = Exec<int, string>(GetInt, 123);
Console.WriteLine(str);

 实际生产中 扩展应用:

        /// <summary>
        /// 执行方法
        /// </summary>
        /// <typeparam name="P">传入参数</typeparam>
        /// <typeparam name="V">返回类型</typeparam>
        /// <param name="Func">方法名称</param>
        /// <param name="param">参数值</param>
        /// <param name="execMaxCount">最大执行次数</param>
        /// <param name="isSleep">是否Sleep</param>
        /// <param name="logname">日志名称</param>
        /// <param name="currentDeveloper">开发者</param>
        /// <param name="level">日志级别</param>
        /// <returns>返回V类型</returns>
        public static V Exec<P,V>(Func<P,V> Func, P param, int execMaxCount, bool isSleep, string logname, Developer currentDeveloper, LogLevel level)
        {
            V res = default(V);
            bool isTrue = true;
            int count = 1;
            while (isTrue)
            {
                if (count <= execMaxCount)
                {
                    try
                    {
                        res = Func(param);
                        //WebLogUtils.LogWrite(logname, level, currentDeveloper, logname + " 执行成功。 执行参数:" + param + "当前执行次数:" + count);
                        isTrue = false;
                    }
                    catch (Exception ex)
                    {
                        //次数记录日志,自己定义要记入的内容
                        //WebLogUtils.LogWrite(logname, level, currentDeveloper, logname + " 执行失败。执行参数:" + param + "当前执行次数:" + count + "异常信息:" + ex.Message + "异常堆栈:" + ex.StackTrace);
                        if (isSleep)
                        {
                           System.Threading.Thread.Sleep(3000 * count);
                        }
                        isTrue = true;
                    }
                    count++;
                }
                else
                {
                    isTrue = false;
                }
            }
            return res;
        }

 

 

  

 

posted @ 2013-10-25 11:40  TakeTry  阅读(490)  评论(0编辑  收藏  举报