How to implement one “catch'em all” exception handler with resume?

前端 未结 9 602
梦如初夏
梦如初夏 2020-11-29 07:07

I wonder how can I write a catch\'em all exception handler in the application level which will give the user the option to resume the application f

相关标签:
9条回答
  • 2020-11-29 07:58

    Use below code in your program.cs class. It will automatically Send mail when exception occurs.

    using System;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Mail;
    using System.Threading; 
    
    namespace ExceptionHandlerTest
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.ThreadException +=
                    new ThreadExceptionEventHandler(Application_ThreadException);
    
                // Your designer generated commands.
            }
    
            static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) 
            {
    
                var fromAddress = new MailAddress("your Gmail address", "Your name");
                var toAddress = new MailAddress("email address where you want to receive reports", "Your name");
                const string fromPassword = "your password";
                const string subject = "exception report";
                Exception exception = e.Exception;
                string body = exception.Message + "\n" + exception.Data + "\n" + exception.StackTrace + "\n" + exception.Source;
    
                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    //You can also use SendAsync method instead of Send so your application begin invoking instead of waiting for send mail to complete. SendAsync(MailMessage, Object) :- Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes. 
                    smtp.Send(message);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 08:01

    If you are running a Windows Forms application: add a handler to the Application.ThreadException event.

    0 讨论(0)
  • 2020-11-29 08:06

    This just screams bad design all over. Never use exceptions for things like this. Exceptions are ONLY to be used when something the programmer did not intend to occures.

    If you want error-handling. dont use exceptions like this, rahter build a system where you save states and can go back to states etc... but using exceptions for state handling, bad idea.

    0 讨论(0)
提交回复
热议问题