hollow1976 发表于 2013-8-13 15:34:22

用自定义异常类及手动抛出异常来进行异常处理

根据网上找到的资料和自己测试的结果总结的,感觉挺方便,还望高手指正.
1,自定义全局异常类
public class CustomExceptionHandler
    {
      public CustomExceptionHandler()
      {
            Application.ThreadException += new ThreadExceptionEventHandler(this.OnThreadException);
      }
      private void OnThreadException(object sender, ThreadExceptionEventArgs args)
      {
            try
            {
                string errorMsg = "程序运行过程中发生错误,错误信息如下:\n";
                errorMsg += args.Exception.Message;
                errorMsg += "\n发生错误的程序集为:";
                errorMsg += args.Exception.Source;
                //errorMsg += "\n发生错误的具体位置为:\n";
                //errorMsg += args.Exception.StackTrace;
                MessageBox.Show(errorMsg, "运行时错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("系统运行时发生致命错误!\n请保存好相关数据,重启系统。", "致命错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
      }
    }


2,主窗体加载前创建自定义异常类实例
static class Program
    {
      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      
      private static void Main()
      {
                XpoDefault.ConnectionString = ConnectionHelper.ConnectionString;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                CustomExceptionHandler customException =new CustomExceptionHandler();
                Application.Run(new Form1());
      }
    }

3,在逻辑层手动抛出可能出现的错误
public pcm_42 GetPcm42ByPartyCode(string code)
      {
            try
            {
                if (string.IsNullOrEmpty(code))
                {
                  throw new Exception("查询条件客户代码为空请输入");
                }
                var query = pcm42service.GetCustomByPartyCode(code);
                if (!query.Any())
                {
                  throw new Exception("没有找到代码为:"+code+"的客户");
                }
                return query.Single();

            }
            catch (Exception ex)
            {
                throw ex;
            }
      }
这样在UI层如果该输入条件的地方没有输入,或没有查询到相关数据程序会自动弹出相应提示.

羽叶 发表于 2013-8-13 15:38:03

这仅仅捕获了UI线程异常,非UI线程的异常呢?

hollow1976 发表于 2013-8-13 16:13:25

天堂羽叶 发表于 2013-8-13 15:38
这仅仅捕获了UI线程异常,非UI线程的异常呢?

天堂老师,这样行吗?
public class CustomExceptionHandler
    {

      public CustomExceptionHandler()
      {
         
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
      }




      private void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                string str = GetExceptionMsg(e.Exception, e.ToString());
                MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
                MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

         
            private string GetExceptionMsg(Exception ex, string backStr)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("****************************异常文本****************************");
                sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
                if (ex != null)
                {
                  sb.AppendLine("【异常类型】:" + ex.GetType().Name);
                  sb.AppendLine("【异常信息】:" + ex.Message);
                  sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
                }
                else
                {
                  sb.AppendLine("【未处理异常】:" + backStr);
                }
                sb.AppendLine("***************************************************************");
                return sb.ToString();
            }
      }

羽叶 发表于 2013-8-13 17:15:36

嗯,这样不错,不过为什么叫我老湿?

hollow1976 发表于 2013-8-13 17:19:42

天堂羽叶 发表于 2013-8-13 17:15
嗯,这样不错,不过为什么叫我老湿?

呵呵,常看你的帖子,确实是老师.
页: [1]
查看完整版本: 用自定义异常类及手动抛出异常来进行异常处理