Coding⏱️ 2 min read📅 2026-05-31

How to Fix: .NET Global exception handler in console application

Global exception handler in console application

Quick Answer: Use the AppDomain.UnhandledException event, but only if targeting .NET Framework 4.0 or later.

To define a global exception handler for unhandled exceptions in a console application, you can use the AppDomain.UnhandledException event and handle it programmatically.

🔧 Proven Troubleshooting Steps

Method 1: Using AppDomain.UnhandledException Event

  1. Step 1: Create a method to handle the unhandled exception.

Method 2: Using AppDomain.FirstChanceException Event

  1. Step 1: Create a method to handle the first chance exception.

✨ Wrapping Up

To implement this, you can add the following code in your Program.cs file:

static void Main(string[] args) {     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);     Console.ReadLine(); } private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e) {     Exception exception = (Exception)e.ExceptionObject;     Console.WriteLine(exception.Message);     Console.WriteLine(exception.StackTrace);     // You can also log the exception or perform any other desired action.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions