Skip to content

AppDomain UnhandledException

Context: The AppDomain.CurrentDomain.UnhandledException event is raised when an exception is not caught in any try-catch block in the default AppDomain. This event is typically used for logging and cleanup before the application terminates. After this event, the application will shut down (for most exceptions). You cannot prevent termination from this event.

using System;
class Program
{
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
Exception ex = (Exception)e.ExceptionObject;
Console.WriteLine($"FATAL: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
// Log to file or event log
Environment.Exit(1);
};
// Simulate an unhandled exception
throw new NullReferenceException("Something is null.");
}
}
Terminal window
dotnet run
FATAL: Something is null.
Stack trace: at Program.Main(String[] args)
  • This event is not fired for exceptions in threads that have their own AppDomain.
  • For WinForms/WPF, also subscribe to Application.ThreadException.
  • The event is a last resort; always try to handle exceptions locally first.

Windows Service – Log unhandled exceptions to Windows Event Log and attempt a graceful shutdown.
See .NET docs on AppDomain.UnhandledException.