zoukankan      html  css  js  c++  java
  • WinForm程序统一捕捉自定义异常

    1:新建自定义异常类

     1  /// <summary>
     2     /// 业务异常类
     3     /// </summary>
     4     public class BusinessException : Exception
     5     {
     6         public BusinessException()
     7         {
     8         }
     9         public BusinessException(string message): base(message)
    10         {
    11         }
    12         public BusinessException(string message, System.Exception inner)
    13             : base(message, inner)
    14         {
    15         }
    16         public BusinessException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context): base(info, context)
    17         {
    18         }
    19     }
    View Code

    2:在Program文件中捕捉异常

     1 /// <summary>
     2         /// 应用程序的主入口点。
     3         /// </summary>
     4         [STAThread]
     5         static void Main()
     6         {
     7             try
     8             {
     9                 //处理未捕获的异常
    10                 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    11                 //处理UI线程异常
    12                 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    13                 //处理非UI线程异常
    14                 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    15                 Application.EnableVisualStyles();
    16                 Application.SetCompatibleTextRenderingDefault(false);
    17                 Application.Run(new Form1());
    18             }
    19             catch (Exception ex)
    20             {
    21                 string str = "";
    22                 string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "
    ";
    23                 if (ex != null)
    24                 {
    25                     str = string.Format(strDateInfo + "异常类型:{0}
    异常消息:{1}
    异常信息:{2}
    ",
    26                     ex.GetType().Name, ex.Message, ex.StackTrace);
    27                 }
    28                 else
    29                 {
    30                     str = string.Format("应用程序线程错误:{0}", ex);
    31                 }
    32                 
    33                 MessageBox.Show("发生致命错误,请及时联系作者!"+str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    34             }
    35         }
    36         /// <summary>
    37         ///这就是我们要在发生未处理异常时处理的方法,我这是写出错详细信息到文本,如出错后弹出一个漂亮的出错提示窗体,给大家做个参考
    38         ///做法很多,可以是把出错详细信息记录到文本、数据库,发送出错邮件到作者信箱或出错后重新初始化等等
    39         ///这就是仁者见仁智者见智,大家自己做了。
    40         /// </summary>
    41         /// <param name="sender"></param>
    42         /// <param name="e"></param>
    43         static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    44         {
    45             string str = "";
    46             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "
    ";
    47             Exception error = e.Exception as Exception;
    48             if (error != null)
    49             {
    50                 str = string.Format(strDateInfo + "异常类型:{0}
    异常消息:{1}
    异常信息:{2}
    ",
    51                 error.GetType().Name, error.Message, error.StackTrace);
    52             }
    53             else
    54             {
    55                 str = string.Format("应用程序线程错误:{0}", e);
    56             }
    57            
    58             MessageBox.Show("发生致命错误,请及时联系作者!"+ str, "系统错误"+ str, MessageBoxButtons.OK, MessageBoxIcon.Warning);
    59         }
    60         static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    61         {
    62             string str = "";
    63             Exception error = e.ExceptionObject as Exception;
    64             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "
    ";
    65             if (error != null)
    66             {
    67                 str = string.Format(strDateInfo + "Application UnhandledException:{0};
    
    堆栈信息:{1}", error.Message, error.StackTrace);
    68             }
    69             else
    70             {
    71                 str = string.Format("Application UnhandledError:{0}", e);
    72             }
    73             
    74             MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!"+ str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    75         }
    76        
    View Code
  • 相关阅读:
    String类之indexOf--->查找某字对应的位置
    5、文件过滤器
    String类之endsWith方法--->检测该字符串以xx为结尾
    for循环
    java-成员方法/变量、类方法/变量等区别
    4、File类之获取方法
    3、File类之创建、删除、重命名、判断方法
    2、创建File类对象
    Java 实现Redis客户端,服务端
    Phoenix踩坑填坑记录
  • 原文地址:https://www.cnblogs.com/baiyundadie/p/11189686.html
Copyright © 2011-2022 走看看