winform 捕获并处理未处理的异常

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(AppThreadException);
            Application.Run(new sqlToXMLtool());
        }

        private static void AppThreadException(object source, System.Threading.ThreadExceptionEventArgs e)
        {
            string errorMsg = string.Format("未处理异常: \n{0}\n", e.Exception.Message);
            errorMsg += Environment.NewLine;

            DialogResult result = MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);

            //如果点击“中止”则退出程序
            if (result == DialogResult.Abort)
            {
                Application.Exit();
            }
        }
    }

posted @ 2011-06-15 17:55  wangjj89621  阅读(254)  评论(0编辑  收藏  举报