Visual Studio breaks on Handled Exception as if it was Unhandled

Viewed 133

I use Activator.CreateInstance to create class from Type. In case where constructor throws exception (That happens and is totally expected) it is caught by try..catch.
But Visual Studio breaks as if it was unhandled.
I managed to simplify this issue:
Screenshot of VS
Code in Tio

using System;

public class Program
{
  public static void Main()
  {
    try {
      var foo = Activator.CreateInstance(typeof(Foo));
    catch {}
    Console.WriteLine("Finished");
  }

  class Foo {
    public Foo() {
      throw new Exception();
    }
  }
}

If that's important I use Visual Studio 2019

1 Answers

That happens because you are debugging your code, even Visual Studio says that the exception is handled.

It is configurable. Go to "Debug -> Windows -> Exception handling" window and uncheck all exceptions in "Common Language Runtime Exceptions" (or leave some if you want to see them even if it is handled).

Another VS settings that may cause it - Options->Debuggin->"Break when exceptions cross AppDomain or managed/native boundaries" enabled

Related