Visual Studio不会在抛出异常时停止

  • 本文关键字:抛出异常 Studio Visual c#
  • 更新时间 :
  • 英文 :


当我有以下代码:

public class Entry
{
    public void Main()
    {
        var p = new Class1();
    }
}
public class Class1
{
    public Class1()
    {
        DoSomething();
    }
    private void DoSomething()
    {
        try
        {
            CallToMethodWhichThrowsAnyException()
        }
        catch (Exception ex)
        {
            throw new CustomException(ex.Message); // where CustomException is simple System.Exception inherited class
        }
    }
}

为什么我的CustomException没有被抛出并停止在条目中调试的执行。主要还是在Class1的构造函数(或在我的DoSomething方法)?

当前窗口中只有A first chance exception of type 'MyLibrary.CustomException' occurred in MyLibrary.dll消息

Visual Studio的异常设置设置为所有CLR异常仅在user - unhandling时抛出。

第一次机会异常消息就是它所说的,第一次机会异常。

在您的情况下,这很可能意味着您已将调试器配置为不会在这种类型的异常时停止。因为这是自定义异常类型,所以这是默认行为。

要在第一次异常时启用中断,请转到Debug -> Exceptions并选择您希望调试器中断的异常类型。

A first chance exception表示某个方法抛出了异常。现在你的代码有机会处理它了。

似乎CallToMethodWhichThrowsAnyException已经处理了CustomException从它内部的某个地方抛出,这就是为什么你不抓住它。

此外,当重新抛出时,您应该包装原始异常,以便堆栈跟踪信息不会丢失:

    catch (Exception ex)
    {
        throw new CustomException(ex.Message, ex); // notice the second argument
    }

最新更新