Poppy Nw
Poppy Nw

Reputation: 33

How to catching Unhandled Exceptions in Console application

I often find unhandled exceptions in my console application. Then later it hangs and stops all processes. I'm also forced to restart the program before it will work normally.

Unhandled Exception:

My program is a server program. I don't want it to stop working, because it affects the business.

How can I handle this error?

Upvotes: 2

Views: 4283

Answers (1)

Keyur Ramoliya
Keyur Ramoliya

Reputation: 1924

Register a global exception handler in your main method like this:

//Add handler to handle the exception raised by additional threads
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

Then handle all the unhandled exception in CurrentDomain_UnhandledException method.

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    //Your logic here, for ex., log the exception somewhere
}

Upvotes: 4

Related Questions