Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - What's the best way to implement a "catch all exceptions handler"

I'm wondering what the best way is to have a "if all else fails catch it".

I mean, you're handling as much exceptions as possible in your application, but still there are bound to be bugs, so I need to have something that catches all unhandled exceptions so I can collect information and store them in a database or submit them to a web service.

Does the AppDomain.CurrentDomain.UnhandledException event capture everything? Even if the application is multithreaded?

Side note: Windows Vista exposes native API functions that allow any application to recover itself after a crash... can't think of the name now... but I'd rather not use it, as many of our users are still using Windows XP.

like image 862
TimothyP Avatar asked Oct 20 '08 19:10

TimothyP


People also ask

What are the 3 approaches to handling exceptions in a web application C#?

try catch finally 2. Use error events to deal with exceptions within the scope of an object. Page_Error Global_Error Application_Error 3. Use custom error pages to display informational messages for unhandled exceptions within the scope of a Web application.

What can we do to handle multiple exceptions in C#?

In C#, You can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions means each catch block is used to handle different type of exception.


1 Answers

I have just played with AppDomain's UnhandledException behavior, (this is the last stage the unhandled exception is registered at)

Yes, after processing the event handlers your application will be terminated and the nasty "... program stopped working dialog" shown.

:) You still can avoid that.

Check out:

class Program {     void Run()     {         AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);          Console.WriteLine("Press enter to exit.");          do         {             (new Thread(delegate()             {                 throw new ArgumentException("ha-ha");             })).Start();          } while (Console.ReadLine().Trim().ToLowerInvariant() == "x");           Console.WriteLine("last good-bye");     }      int r = 0;      void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)     {         Interlocked.Increment(ref r);         Console.WriteLine("handled. {0}", r);         Console.WriteLine("Terminating " + e.IsTerminating.ToString());          Thread.CurrentThread.IsBackground = true;         Thread.CurrentThread.Name = "Dead thread";                      while (true)             Thread.Sleep(TimeSpan.FromHours(1));         //Process.GetCurrentProcess().Kill();     }      static void Main(string[] args)     {         Console.WriteLine("...");         (new Program()).Run();     } } 

P.S. Do handle the unhandled for Application.ThreadException (WinForms) or DispatcherUnhandledException (WPF) at the higher level.

like image 182
bohdan_trotsenko Avatar answered Sep 28 '22 02:09

bohdan_trotsenko