How to catch Unhandled Exception ?


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6.  
  7. namespace WindowsFormsApplication2 {
  8.     static class Program {
  9.         /// <summary>
  10.         /// The main entry point for the application.
  11.         /// </summary>
  12.         [STAThread]
  13.         static void Main() {
  14.             Application.EnableVisualStyles();
  15.             Application.SetCompatibleTextRenderingDefault(false);
  16.             Form1 form1 = new Form1();
  17.             Application.ThreadException += new ThreadExceptionEventHandler(form1.UnhandledThreadExceptionHandler);
  18.             Application.Run(form1);
  19.         }
  20.     }
  21. }
  22.        
  23. using System;
  24. using System.Collections.Generic;
  25. using System.ComponentModel;
  26. using System.Data;
  27. using System.Drawing;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Threading;
  31. using System.Windows.Forms;
  32.  
  33. namespace WindowsFormsApplication2 {
  34.     public partial class Form1 : Form {
  35.         public Form1() {
  36.             InitializeComponent();
  37.         }
  38.  
  39.         public void UnhandledThreadExceptionHandler(object sender, ThreadExceptionEventArgs e) {
  40.             this.HandleUnhandledException(e.Exception);
  41.         }
  42.  
  43.         public void HandleUnhandledException(Exception e) {
  44.             // do what you want here.
  45.             if (MessageBox.Show("An unexpected error has occurred. Continue?",
  46.                 "My application", MessageBoxButtons.YesNo, MessageBoxIcon.Stop,
  47.                 MessageBoxDefaultButton.Button2) == DialogResult.No) {
  48.                 Application.Exit();
  49.             }
  50.         }
  51.  
  52.         private void button1_Click(object sender, EventArgs e) {
  53.             throw new ApplicationException("Exception");
  54.         }
  55.  
  56.     }
  57. }
  58.        
  59. try
  60. {
  61.   MyMethodThatMightThrow();
  62. }
  63. catch(Exception ex)
  64. {
  65.    bool rethrow = ExceptionPolicy.HandleException(ex, "SomePolicy");
  66.    if (rethrow) throw;
  67. }
  68.        
  69. using System;
  70. using System.Windows.Forms;
  71. using System.Net;
  72. using System.Net.Mail;
  73. using System.Threading;
  74.  
  75. namespace ExceptionHandlerTest
  76. {
  77.     static class Program
  78.     {
  79.         /// <summary>
  80.         /// The main entry point for the application.
  81.         /// </summary>
  82.         [STAThread]
  83.         static void Main()
  84.         {
  85.             Application.ThreadException +=
  86.                 new ThreadExceptionEventHandler(Application_ThreadException);
  87.  
  88.             // Your designer generated commands.
  89.         }
  90.  
  91.         static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  92.         {
  93.  
  94.             var fromAddress = new MailAddress("your Gmail address", "Your name");
  95.             var toAddress = new MailAddress("email address where you want to receive reports", "Your name");
  96.             const string fromPassword = "your password";
  97.             const string subject = "exception report";
  98.             Exception exception = e.Exception;
  99.             string body = exception.Message + "n" + exception.Data + "n" + exception.StackTrace + "n" + exception.Source;
  100.  
  101.             var smtp = new SmtpClient
  102.             {
  103.                 Host = "smtp.gmail.com",
  104.                 Port = 587,
  105.                 EnableSsl = true,
  106.                 DeliveryMethod = SmtpDeliveryMethod.Network,
  107.                 UseDefaultCredentials = false,
  108.                 Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  109.             };
  110.             using (var message = new MailMessage(fromAddress, toAddress)
  111.             {
  112.                 Subject = subject,
  113.                 Body = body
  114.             })
  115.             {
  116.                 //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.
  117.                 smtp.Send(message);
  118.             }
  119.         }
  120.     }
  121. }


Learn More :