C WinForm catches global variable exceptions SamWang solution

  • 2020-05-07 20:18:08
  • OfStack

Many small company projects lack exception handling modules, and so do we. This is often the case, the user in UI interface operation, directly out of the stack call exception information dialog box, the boss saw that called 1 fire! How come your code is out of order every day? Ha ha! This is the result of the lack of exception capture handling. Many people who write code today have no awareness of exception handling, as long as the functionality is implemented, and so are many of my team members.

The project just took over, so I plan to do a global exception capture, unified 1 processing mode, using the specific details of the dialog box reminder and log file saving. The following is a change to the C#winform global exception capture found on the web. (after all the exception handling of the project is completed, I will make a record of my experience. Here, I will temporarily make a record of global exception capture.)
 
static class Program 
{ 
/// <summary> 
///  The main entry point for the application.  
/// </summary> 
[STAThread] 
static void Main() 
{ 
try 
{ 
// Set the way the application handles exceptions: ThreadException To deal with  
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 
// To deal with UI Abnormal thread  
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 
// The non UI Abnormal thread  
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 

#region  The main entry point for the application  
Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false); 
Application.Run(new Form1()); 
#endregion 
} 
catch (Exception ex) 
{ 
string str = GetExceptionMsg(ex,string.Empty); 
MessageBox.Show(str, " System error ", MessageBoxButtons.OK, MessageBoxIcon.Error); 
} 
} 


static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
{ 
string str = GetExceptionMsg(e.Exception, e.ToString()); 
MessageBox.Show(str, " System error ", MessageBoxButtons.OK, MessageBoxIcon.Error); 
//LogManager.WriteLog(str); 
} 

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); 
MessageBox.Show(str, " System error ", MessageBoxButtons.OK, MessageBoxIcon.Error); 
//LogManager.WriteLog(str); 
} 

/// <summary> 
///  Generate custom exception messages  
/// </summary> 
/// <param name="ex"> The exception object </param> 
/// <param name="backStr"> Standby exception message: when ex for null Effective when </param> 
/// <returns> Exception string text </returns> 
static string GetExceptionMsg(Exception ex,string backStr) 
{ 
StringBuilder sb = new StringBuilder(); 
sb.AppendLine("**************************** Abnormal text ****************************"); 
sb.AppendLine(" [appearance time] : " + DateTime.Now.ToString()); 
if (ex != null) 
{ 
sb.AppendLine(" [exception type] : " + ex.GetType().Name); 
sb.AppendLine(" [abnormal information] : " + ex.Message); 
sb.AppendLine(" Stack call: " + ex.StackTrace); 
} 
else 
{ 
sb.AppendLine(" [unhandled exception] : " + backStr); 
} 
sb.AppendLine("***************************************************************"); 
return sb.ToString(); 
} 
} 

Reference:
 
static class Program 
{ 
/// <summary> 
///  The main entry point for the application.  
/// </summary> 
[STAThread] 
static void Main() 
{ 
try 
{ 
// Handles uncaught exceptions  
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 
// To deal with UI Abnormal thread  
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 
// The non UI Abnormal thread  
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 

#region  The main entry point for the application  

Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false); 
Application.Run(new Main()); 

#endregion 

} 
catch (Exception ex) 
{ 
string str = ""; 
string strDateInfo = " An exception not handled by the application occurs: " + DateTime.Now.ToString() + "\r\n"; 

if (ex != null) 
{ 
str = string.Format(strDateInfo + " Exception type: {0}\r\n Exception message: {1}\r\n Abnormal information: {2}\r\n", 
ex.GetType().Name, ex.Message, ex.StackTrace); 
} 
else 
{ 
str = string.Format(" Application thread error :{0}", ex); 
} 

//MessageBox.Show(str, " System error ", MessageBoxButtons.OK, MessageBoxIcon.Error); 
LogManager.WriteLog(str); 
} 

} 

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
{ 
string str = ""; 
string strDateInfo = " An exception not handled by the application occurs: " + DateTime.Now.ToString() + "\r\n"; 
Exception error = e.Exception as Exception; 
if (error != null) 
{ 
str = string.Format(strDateInfo + " Exception type: {0}\r\n Exception message: {1}\r\n Abnormal information: {2}\r\n", 
error.GetType().Name, error.Message, error.StackTrace); 
} 
else 
{ 
str = string.Format(" Application thread error :{0}", e); 
} 

//MessageBox.Show(str, " System error ", MessageBoxButtons.OK, MessageBoxIcon.Error); 
LogManager.WriteLog(str); 
} 

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
string str = ""; 
Exception error = e.ExceptionObject as Exception; 
string strDateInfo = " An exception not handled by the application occurs: " + DateTime.Now.ToString() + "\r\n"; 
if (error != null) 
{ 
str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r The stack information :{1}", error.Message, error.StackTrace); 
} 
else 
{ 
str = string.Format("Application UnhandledError:{0}", e); 
} 

//MessageBox.Show(str, " System error ", MessageBoxButtons.OK, MessageBoxIcon.Error); 
LogManager.WriteLog(str); 
} 
} 

Related articles: