C#_WinForm에서 처리되지 않은 예외 캡처

2464 단어 WinForm
QQ처럼 프로그램이 붕괴되어 강제로 종료되기 전에 이 이상을 포착하고 우호적인 인터페이스에서 벗어나 알림을 한다.Program에서cs에 관련 코드를 추가하면 전역의 모든 지점에 붕괴가 발생하면 이곳에서 포착될 수 있습니다.
 
static class Program
{
   /// <summary>
   ///          。
   /// </summary>
  [STAThread]
  static void Main()
  {
     try
     {
         //           
          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);
         Application.Run(new frmlogin());
     }
     catch(Exception ex)
     {
         frmBug f = new frmBug(ex.Message);//      
          f.ShowDialog();
     }

  }
        
  static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)   
  {
      string str = "";
      Exception error = e.Exception as Exception;   
      if (error != null)   
      {   
          str =string.Format("            
:{0}
:{1}
:{2}
", error.GetType().Name, error.Message, error.StackTrace); } else { str =string.Format(" :{0}", e); } frmBug f = new frmBug(str);// f.ShowDialog(); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = ""; Exception error = e.ExceptionObject as Exception; if (error != null) { str =string.Format("Application UnhandledException:{0};
:{1}", error.Message, error.StackTrace); } else { str =string.Format("Application UnhandledError:{0}", e); } frmBug f = new frmBug(str);// f.ShowDialog(); } }

좋은 웹페이지 즐겨찾기