公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

【C#.net】WinForm捕獲全局異常(捕獲未處理的異常)

admin
2024年3月19日 11:39 本文熱度 1307

背景

我們?cè)谧鯳inForm程序的時(shí)候,一般都是對(duì)異常進(jìn)行處理,但是,我們要防止不小心出現(xiàn)未知異常,導(dǎo)致軟件崩潰。也可采集系統(tǒng)未知的異常信息,防止出現(xiàn)異常,也無(wú)法下手。于是就有了如這篇文章標(biāo)題所述的一個(gè)簡(jiǎn)單的需求。

代碼實(shí)現(xiàn)

1、處理未捕獲的異常

  /// <summary>    ///這就是我們要在發(fā)生未處理異常時(shí)處理的方法,我這是寫出錯(cuò)詳細(xì)信息到文本,如出錯(cuò)后彈出一個(gè)漂亮的出錯(cuò)提示窗體,給大家做個(gè)參考    ///做法很多,可以是把出錯(cuò)詳細(xì)信息記錄到文本、數(shù)據(jù)庫(kù),發(fā)送出錯(cuò)郵件到作者信箱或出錯(cuò)后重新初始化等等    ///這就是仁者見仁智者見智,大家自己做了。    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
     string str = "";      string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n";      Exception error = e.Exception as Exception;      if (error != null) {        str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n",           error.GetType().Name, error.Message, error.StackTrace);      }      else {        str = string.Format("應(yīng)用程序線程錯(cuò)誤:{0}", e);      }      Helper.GetInstance().PlanLog(str, LogType.應(yīng)用程序異常.ToString());        //frmBug f = new frmBug(str);//友好提示界面      //f.ShowDialog();      MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);    }

2、處理UI線程異常

    /// <summary>
    /// 應(yīng)用程序的主入口點(diǎn)。
    /// </summary>
    [STAThread]
    static void Main() {
      try {
        //可定義多個(gè)線程
        Thread _UserMessageThread;
        _UserMessageThread = new Thread(new ThreadStart(LoginManager.GetInstance().test));
        _UserMessageThread.IsBackground = true;
        _UserMessageThread.Start();
        //處理未捕獲的異常
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        //處理UI線程異常
          Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
        //處理非UI線程異常
          AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Xw.Common.Sys.SysConfig.AppExwcutePath = Application.ExecutablePath;
        Xw.Common.Sys.SysConfig.AppStartPath = Application.StartupPath;
        Xw.Common.Sys.SysConfig.Version = "V1.0.0";
        Xw.Common.Sys.SysConfig.SoftFullName = "拍鞋網(wǎng)";
        Xw.Common.Sys.SysConfig.SoftName = "軟件園店";
        if (!Xw.Common.Sys.SysConfig.IsOnlyRunSoft("PaiXie.Pos.Client")) {
          Xw.Common.Sys.MsgBoxWin.ShowInformation("該程序已運(yùn)行!");
          return;
        }
        Application.Run(new Login());
      }
      catch (Exception ex) {
        string str = "";
        string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n";
        if (ex != null) {
          str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n",
             ex.GetType().Name, ex.Message, ex.StackTrace);
        }
        else {
          str = string.Format("應(yīng)用程序線程錯(cuò)誤:{0}", ex);
        }
        Helper.GetInstance().PlanLog(str, LogType.應(yīng)用程序異常.ToString());
            //frmBug f = new frmBug(str);//友好提示界面
        //f.ShowDialog();
        MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      }

3、處理非UI線程異常

  /// <summary>    /// 應(yīng)用程序的主入口點(diǎn)。    /// </summary>    [STAThread]    static void Main() {      try {        //可定義多個(gè)線程            Thread _UserMessageThread;                           _UserMessageThread = new Thread(new ThreadStart(LoginManager.GetInstance().test));        _UserMessageThread.IsBackground = true;        _UserMessageThread.Start();
       //處理未捕獲的異常          Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);        //處理UI線程異常          Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);        //處理非UI線程異常          AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);        Application.EnableVisualStyles();        Application.SetCompatibleTextRenderingDefault(false);        Xw.Common.Sys.SysConfig.AppExwcutePath = Application.ExecutablePath;        Xw.Common.Sys.SysConfig.AppStartPath = Application.StartupPath;        Xw.Common.Sys.SysConfig.Version = "V1.0.0";        Xw.Common.Sys.SysConfig.SoftFullName = "拍鞋網(wǎng)";        Xw.Common.Sys.SysConfig.SoftName = "軟件園店";        if (!Xw.Common.Sys.SysConfig.IsOnlyRunSoft("PaiXie.Pos.Client")) {          Xw.Common.Sys.MsgBoxWin.ShowInformation("該程序已運(yùn)行!");          return;        }        Application.Run(new Login());      }      catch (Exception ex) {        string str = "";        string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n";        if (ex != null) {          str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n",             ex.GetType().Name, ex.Message, ex.StackTrace);        }        else {          str = string.Format("應(yīng)用程序線程錯(cuò)誤:{0}", ex);        }        Helper.GetInstance().PlanLog(str, LogType.應(yīng)用程序異常.ToString());            //frmBug f = new frmBug(str);//友好提示界面        //f.ShowDialog();        MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);      }      }

完整代碼

using PaiXie.Pos.Client.Core;using PaiXie.Utils;using System;using System.Collections.Generic;using System.Linq;using System.Threading;using System.Windows.Forms;namespace PaiXie.Pos.Client {  static class Program {     /// <summary>    /// 應(yīng)用程序的主入口點(diǎn)。    /// </summary>    [STAThread]    static void Main() {      try {        //可定義多個(gè)線程            Thread _UserMessageThread;                           _UserMessageThread = new Thread(new ThreadStart(LoginManager.GetInstance().test));        _UserMessageThread.IsBackground = true;        _UserMessageThread.Start();
       //處理未捕獲的異常          Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);        //處理UI線程異常          Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);        //處理非UI線程異常          AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);        Application.EnableVisualStyles();        Application.SetCompatibleTextRenderingDefault(false);        Xw.Common.Sys.SysConfig.AppExwcutePath = Application.ExecutablePath;        Xw.Common.Sys.SysConfig.AppStartPath = Application.StartupPath;        Xw.Common.Sys.SysConfig.Version = "V1.0.0";        Xw.Common.Sys.SysConfig.SoftFullName = "拍鞋網(wǎng)";        Xw.Common.Sys.SysConfig.SoftName = "軟件園店";        if (!Xw.Common.Sys.SysConfig.IsOnlyRunSoft("PaiXie.Pos.Client")) {          Xw.Common.Sys.MsgBoxWin.ShowInformation("該程序已運(yùn)行!");          return;        }        Application.Run(new Login());      }      catch (Exception ex) {        string str = "";        string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n";        if (ex != null) {          str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n",             ex.GetType().Name, ex.Message, ex.StackTrace);        }        else {          str = string.Format("應(yīng)用程序線程錯(cuò)誤:{0}", ex);        }        Helper.GetInstance().PlanLog(str, LogType.應(yīng)用程序異常.ToString());            //frmBug f = new frmBug(str);//友好提示界面        //f.ShowDialog();        MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);      }      }    /// <summary>    ///這就是我們要在發(fā)生未處理異常時(shí)處理的方法,我這是寫出錯(cuò)詳細(xì)信息到文本,如出錯(cuò)后彈出一個(gè)漂亮的出錯(cuò)提示窗體,給大家做個(gè)參考    ///做法很多,可以是把出錯(cuò)詳細(xì)信息記錄到文本、數(shù)據(jù)庫(kù),發(fā)送出錯(cuò)郵件到作者信箱或出錯(cuò)后重新初始化等等    ///這就是仁者見仁智者見智,大家自己做了。    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
     string str = "";      string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n";      Exception error = e.Exception as Exception;      if (error != null) {        str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n",           error.GetType().Name, error.Message, error.StackTrace);      }      else {        str = string.Format("應(yīng)用程序線程錯(cuò)誤:{0}", e);      }      Helper.GetInstance().PlanLog(str, LogType.應(yīng)用程序異常.ToString());        //frmBug f = new frmBug(str);//友好提示界面      //f.ShowDialog();      MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);    }    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {      string str = "";      Exception error = e.ExceptionObject as Exception;      string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n";      if (error != null) {        str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆棧信息:{1}", error.Message, error.StackTrace);      }      else {        str = string.Format("Application UnhandledError:{0}", e);      }      Helper.GetInstance().PlanLog(str, LogType.應(yīng)用程序異常.ToString());        //frmBug f = new frmBug(str);//友好提示界面      //f.ShowDialog();      MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)停止當(dāng)前操作并及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);    }  }}

總結(jié)

針對(duì)異常,我們肯定無(wú)法事先全部預(yù)知,所以進(jìn)行全局異常捕獲還是很有必要的。


該文章在 2024/3/19 11:39:14 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产欧美国日产高清视频 | 国产精品视频一区二区五区 | 97人妻超级碰碰碰 | 国产精品三级在线观看无码 | 国产高清视频a在线大全 | 国产精品va在线观看无码电影 | 精品国产在天天在线观看 | 国产精品爆乳奶水 | 成人a片在线观看免费播放 成人a影片在线观看 | 国产成人无码a区在线观看视频免费 | 国产精品福利短视在线播放 | 国产亚洲中文日本不卡2区 国产亚洲中文日本不卡二区 | 韩国免费a级作爱片在线观看 | 韩国高清一区二区午夜无码 | 国产免费午夜a无码v | 国产在线观看d妇在野外 | 国产一区在线免费观看 | 精品国产片自在线拍免费看 | av検索データベース | 国产精品女a片爽爽波多洁 国产精品女a色欲av色欲老师 | 高潮流白浆潮喷在线播放视频 | 国产不卡视频一区二区 | 成人日韩高清 | 国产精品无码一区二区三区不卡 | 国产偷窥熟女高潮精品视频免费 | 国产亚洲自拍一区在线观看 | 国产欧美日韩制服丝袜三区 | 国产高清在线精品一区a | 成人无码a区在线观看视 | 国产一级无码视频 | 成人综合网站在线 | 国产高潮流白浆啊免费a片动态 | av一区二区三区在线播放 | 国产精品欧美一区二区三区四区 | www国产无套 | 91精品福利 | 国产精品亚洲综合一区 | 国产色无码精品视频国产 | 国产成人免费高清av | av无码 | av无码精品一区二区久久 |