Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling unhandled exceptions problem

Tags:

c#

exception

I wanted to set some handler for all the unexpected exceptions that I might not have caught inside my code. In Program.Main() I used the following code:

AppDomain.CurrentDomain.UnhandledException     += new UnhandledExceptionEventHandler(ErrorHandler.HandleException); 

But it didn't work as I expected. When I started the application in debugging mode and threw an exception it did call the handler, but afterwards the exception helper in Visual Studio popped up as if the exception occurred without any handling. I tried Application.Exit() inside the handler but it didn't work as well.

What I would like to achieve is that the exception is handled with my handler and then the application closes nicely. Is there any other way to do it or am I using the code above in the wrong way?

like image 787
agnieszka Avatar asked Jan 02 '09 09:01

agnieszka


People also ask

What happens if there is an unhandled exception?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.

Which event is used for unhandled exception?

The UnhandledException event is raised for unhandled exceptions thrown in other threads. Starting with Microsoft Visual Studio 2005, the Visual Basic application framework provides another event for unhandled exceptions in the main application thread.

Is unhandled exception a crash?

The reports in the Unhandled Exceptions are C# exceptions that your application is not catching in a try-catch block. These do not result in a crash (the application keeps running), but may result in unexpected behavior.


2 Answers

Normally I use something like this to try and catch all unexpected top-level exceptions.

using System;  static class Program {   [STAThread]   static void Main(string[] argv)   {     try     {       AppDomain.CurrentDomain.UnhandledException += (sender,e)       => FatalExceptionObject(e.ExceptionObject);        Application.ThreadException += (sender,e)       => FatalExceptionHandler.Handle(e.Exception);        // whatever you need/want here        Application.Run(new MainWindow());     }     catch (Exception huh)     {       FatalExceptionHandler.Handle(huh);     }   }    static void FatalExceptionObject(object exceptionObject) {     var huh = exceptionObject as Exception;     if (huh == null) {       huh = new NotSupportedException(         "Unhandled exception doesn't derive from System.Exception: "          + exceptionObject.ToString()       );     }     FatalExceptionHandler.Handle(huh);   } } 

Maybe it is something you find helpful too? This main code routes all three ways of catching unexpected top-level exceptions through one method call. All you now need is a static class FatalExceptionHandler that includes your top-level exception handling in its Handle method.

And really, any application developer knows there are really just two things to do there:

  1. Show/log the exception like you see fit
  2. Make sure you exit/kill the application process

If you think item two is strange, remember that we only bother to do this in the first place for really exceptional situations. These things are probably bugs that need changes to your application to be accurately addressed. Any other exception handling - the functional kind - should be lower down inside your actual program code, catching specific kinds of exceptions where this makes sense and handling them there in the way that makes sense. Anything else should bubble up to your FatalExceptionHandler to make itself known and stop the possibly crippled program from working from corrupted state

Dead programs tell no lies... ;-)

like image 109
peSHIr Avatar answered Sep 28 '22 02:09

peSHIr


It's because you're running it through Visual Studio in Debug mode. If you release and install your app somewhere else, nothing but your global exception handler will be processed.

like image 23
Peter Lindholm Avatar answered Sep 28 '22 03:09

Peter Lindholm