-6

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.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Herbert Feichtinger
  • 165
  • 1
  • 2
  • 11
  • 7
    Please post the code in your post. Links to code are harder to follow and reply to. – Hank Oct 17 '16 at 16:01
  • 1
    You should try harder and find the pattern. If `try catch` will help for Exception #1, guess what will help for Exception #2 ... ? – Peter B Oct 17 '16 at 16:03

1 Answers1

2

See the code in the link of your question:

// The example displays the following output:
//       Catch clause caught : 1
//       
//       MyHandler caught : 2
//       Runtime terminating: True
//       
//       Unhandled Exception: System.Exception: 2
//          at Example.Main()  

As you can see, nobody is handling the 2nd exception. Quoting the article:

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • I asked if somebody knows how to get back the old behavior which allows to silently handle an unhandled exception from any domain. The user should not see the 2nd exception because it is unhandled. – Herbert Feichtinger Oct 18 '16 at 06:54
  • I also remember that there is/was a config dialog to change the behaviour of exceptions (in VB.NET at least). I cannot find it at the moment. Maybe it is gone. – Herbert Feichtinger Oct 18 '16 at 07:05
  • 1
    @HerbertFeichtinger maybe you refer to this? http://stackoverflow.com/a/5762806/3932049 – Camilo Terevinto Oct 18 '16 at 10:47