Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio breaks on Handled Exception as if it was Unhandled

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

like image 210
Wokarol Avatar asked Oct 19 '25 10:10

Wokarol


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

like image 63
Wizard91 Avatar answered Oct 22 '25 00:10

Wizard91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!