.Net Global Exception Handler in Console Application

.NET Global exception handler in console application

No, that's the correct way to do it. This worked exactly as it should, something you can work from perhaps:

using System;

class Program {
static void Main(string[] args) {
System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
throw new Exception("Kaboom");
}

static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) {
Console.WriteLine(e.ExceptionObject.ToString());
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
Environment.Exit(1);
}
}

Do keep in mind that you cannot catch type and file load exceptions generated by the jitter this way. They happen before your Main() method starts running. Catching those requires delaying the jitter, move the risky code into another method and apply the [MethodImpl(MethodImplOptions.NoInlining)] attribute to it.

.NET Core Global exception handler in console application

You're right, the AppDomain.UnhandledException or it's analog will be available only in .Net Core 2.0, so for now you should either wait or add additional try/catch blocks. However, if you're using the tasks, you can use TaskScheduler.UnobservedTaskException, which is available from first version of .Net Core.

How to catching Unhandled Exceptions in Console application

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
}

Global exception handling in c# (console application)

Handling exceptions globally is good for certain tasks only, such as logging errors or graceful exit of the application.

In all other cases, it is recommended that you handle the exception in the most relevant place. For example, if you make a web service call from your app and if the service is not available, you should handle that exception where you make the webservice call instead of catching it globally. This improves the readability of the code, and also it's not practical to do anything sensible at global level as you have very little information on where the exception originated. (Of course you have the stack trace, but its not readily available for programmatic manipulation)

However, you can add an application wide exception handler by attaching a handler to AppDomain.UnhandledException as also indicated in the comment by @Jesse

Non-terminating global exception handler in console applications

This is such a horribly bad idea that I wouldn't be surprised if it was purposely made very difficult or impossible to accomplish.

If the Unhandled exception handler ever gets called, your program is by definition in an unknown and most likely corrupt state. The only reasonable thing to do is to terminate the program as quickly as possible, after logging the error and maybe trying to save any critical data. Even that's a risk, because the error could have happened in the logger ... or in saving data during normal shutdown.

You want to catch the divide by 0 exception? And are you going to check where that exception occurred so that you only catch your own errors. Or is it okay if you blindly ignore when somebody else's code throws a divide by 0 exception?

And even if you can do that, what are you going to do when the exception occurs? It's not like you can just jump back to where you were and continue. The stack has been unwound and all context lost.

You do not want to do this. Really. Write your code so that it won't divide by 0. Or use try/catch. That's what it's for.

Catching exceptions in console application C#

Does all that run in a loop of some kind? Or on a timer?

Perhaps put a try-catch around the body of the loop or the method that runs all your code, add a logging framework of your choice (log4net or nlog seem good) and then in the catch log the exception. Most logging frameworks allow you to include the exception and will include stacktrace, etc.

Putting debug logging throughout the process can also help to narrow down where it's happening.

Can I create a global exception handler in C# that lets the code continue running afterward?

You can write a global exception handler method that you call in every catch block, which writes the stack trace where ever you want to save it. But you'd need to write try . . . catch blocks for every operation that needs them and call the exception handler in each.

You can also call that global exception handler method in the MyApplication.UnhandledException handler for all unhandled events. But when control gets to that method in that case, the program is not going to continue running.



Related Topics



Leave a reply



Submit