Form tool class (3) Form whole process exception capture

In the development, there are some commonly used methods. It is troublesome to find the previously written ones every time. If you write them yourself, you are doing repetitive work, so there is always a small tool class and you can add content at any time.

3. The whole process of winform form exception capture static class  FormTools (public static class FormTools)

        /// <summary>
        /// Exception handling method
        /// </summary>
        public static void ExceptionControl()
        {
            / / Set the application to handle exceptions: ThreadException handling  
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //handle UI thread exception  
            Application.ThreadException += (sender, e) =>
            {
                var str = GetExceptionMsg(e.Exception, e.ToString());
                var messageForm =new ErrorMessageForm();
                messageForm.SetMessage(str);
                var flg = FormIsShow (messageForm.Name);
                if (flg) return;
                messageForm.ShowDialog();
            };
            //Handle non-UI thread exceptions  
            AppDomain.CurrentDomain.UnhandledException += (sender, e)=>
            {
                var str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
                var messageForm = new ErrorMessageForm();
                messageForm.SetMessage(str);
                var flg = FormIsShow (messageForm.Name);
                if (flg) return;
                messageForm.ShowDialog();
            };

        }
        /// <summary>  
        /// Generate custom exception message  
        /// </summary>  
        /// <param name="ex">Exception object</param>  
        /// <param name="backStr">Backup exception message: valid when ex is null</param>  
        /// <returns>Exception string text</returns>  
        private static string GetExceptionMsg(Exception ex, string backStr)
        {
            var sb = new StringBuilder ();
            if (ex == null)
            {
                sb.AppendLine("【errmsg】:" + backStr);
            }
            else
            {
                if (ex.GetType().Name.ToLower() == "exception")
                {
                    return ex.Message;
                }
                sb.AppendLine("【msgtime】:" + DateTime.Now);
                sb.AppendLine("【msgtype】:" + ex.GetType().Name);
                sb.AppendLine("【callstack】:" + ex.Message);
                sb.AppendLine("【untreated】:" + ex.StackTrace);
            }
            return sb.ToString();
        }

transfer

FormTools.ExceptionControl();

call screenshot


Notice:

There is the following code in the processing method, which is a message prompt box written by myself

                var messageForm =new ErrorMessageForm();
                messageForm.SetMessage(str);
                var flg = FormIsShow (messageForm.Name);
                if (flg) return;
                messageForm.ShowDialog();

can be replaced with the following code

                MessageBox.Show(str);

In this way, as long as the error generated by the program can be displayed in the form of a prompt box, it will not crash directly and so on. In fact, I think that this mechanism of exceptions can be used for message prompts and message display. Then you can make a beautiful message prompt box, which can avoid the phenomenon that the prompt information is different from the skin color and style of the form in the whole program, and can also avoid the boss "speaking garbled characters".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325627578&siteId=291194637