Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. namespace travelExport
  8. {
  9. internal static class Program
  10. {
  11. static Home home = null;
  12. /// <summary>
  13. /// 应用程序的主入口点。
  14. /// </summary>
  15. [STAThread]
  16. static void Main()
  17. {
  18. try
  19. {
  20. //设置应用程序处理异常方式:ThreadException处理
  21. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  22. //处理UI线程异常
  23. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  24. //处理非UI线程异常
  25. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  26. Application.EnableVisualStyles();
  27. Application.SetCompatibleTextRenderingDefault(false);
  28. home = new Home();
  29. Application.Run(home);
  30. }
  31. catch (Exception ex)
  32. {
  33. string str = GetExceptionMsg(ex, string.Empty);
  34. MessageBoxEx.Show( home ,str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  35. }
  36. }
  37. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  38. {
  39. string str = GetExceptionMsg(e.Exception, e.ToString());
  40. MessageBoxEx.Show(home, str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  41. //LogManager.WriteLog(str);
  42. }
  43. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  44. {
  45. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  46. MessageBoxEx.Show(home, str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  47. //LogManager.WriteLog(str);
  48. }
  49. /// <summary>
  50. /// 生成自定义异常消息
  51. /// </summary>
  52. /// <param name="ex">异常对象</param>
  53. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  54. /// <returns>异常字符串文本</returns>
  55. static string GetExceptionMsg(Exception ex, string backStr)
  56. {
  57. StringBuilder sb = new StringBuilder();
  58. sb.AppendLine("****************************异常文本****************************");
  59. sb.AppendLine("**************************请联系管理员****************************");
  60. sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
  61. if (ex != null)
  62. {
  63. sb.AppendLine("【异常类型】:" + ex.GetType().Name);
  64. sb.AppendLine("【异常信息】:" + ex.Message);
  65. //sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
  66. }
  67. else
  68. {
  69. sb.AppendLine("【未处理异常】:" + backStr);
  70. }
  71. sb.AppendLine("***************************************************************");
  72. return sb.ToString();
  73. }
  74. }
  75. }