-1

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);           
}     
BartoszKP
  • 34,786
  • 15
  • 102
  • 130

1 Answers1

-1

It's all good to setup a global Exception handler, but you still need to use the C# mechanics, try...catch.

If it throws an exception and the global handler catches it, it won't resume at the point of the exception because it doesn't know how to.

The clue is therefore in the name, it's for unhandled exceptions (that aren't caught), and is normally used for logging and graceful cleardown, not a C# equivalent of Visual Basics Resume on Error mechanic.

Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
  • NB: This answer is based on the assumption I get from reading your code, you should really add some detail explain your reasoning and your expectations from this code. – Russ Clarke Oct 06 '15 at 09:25