Why is this UnhandledException event handler not working?

时间:2015-10-06 09:04:44

标签: c# uncaughtexceptionhandler

Not able to execute code after exception, it is printing

ccccc

but does not print

AFTER_EXCEPTION

The code is showing the caught exception and then exists.

static void Main(string[] args)
{      
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); // using System.Diagnostics;

    //   Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnThreadException);

    Process p = Process.GetProcessById(1000);

    Console.WriteLine("AFTER_EXCEPTION");

    Console.ReadLine();
}

static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
    Console.WriteLine("cccc");

    Exception temp = (Exception)args.ExceptionObject;
    Console.WriteLine("MyHandler caught : " + temp.Message);
    Console.WriteLine("MyHandler caught : " + temp.TargetSite);           
}     

1 个答案:

答案 0 :(得分:-1)

设置全局异常处理程序一切都很好,但你仍然需要使用C#技巧,试试...... catch。

如果它抛出异常并且全局处理程序捕获它,它就不会在异常点恢复,因为它不知道如何。

因此,线索是名称,它是未处理的异常(没有被捕获),并且通常用于记录和正常清除,而不是C#等效的Visual Basics Resume on Error mechanic