Visual Studio breaks on Handled Exception as if it was Unhandled

廉价感情. 提交于 2021-02-10 14:20:30

问题


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:


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



来源:https://stackoverflow.com/questions/58466136/visual-studio-breaks-on-handled-exception-as-if-it-was-unhandled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!