I am using the sample code from this MSDN article.
[SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
public static void Main()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
try {
throw new Exception("1");
} catch (Exception e) {
Console.WriteLine("Catch clause caught : {0} \n", e.Message);
}
throw new Exception("2");
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
}
The handler catches the unhandled exception. However after the handler ran, the unhandled exception 2 is still displayed. In debug mode, in release mode and if the .exe is started directly.
I want to suppress the 2nd exception, so the unhandled exception handler can terminate/restart the app silently. I remember this worked in .NET 3 using VB.NET.