2013-03-14 63 views
2

异常:的Visual Studio不停止在被抛出

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不会抛出和Entry.Main或Class 1的构造停止调试的执行(或在我的DoSomething方法中)?

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

Visual Studio的异常设置被设置为所有CLR异常仅在用户未处理时抛出。

+1

'CallToMethodWhichThrowsAnyException'已经在处理'NullReferenceException' – 2013-03-14 13:26:17

+0

是的,但是在catch块中我抛出了一个新的异常,我期待着我的程序在任何时候都会崩溃。但是这个例外只是“吞噬”,而执行将继续。 – 2013-03-14 13:27:34

+1

很难分辨您发布的内容。在实例化Class1的行上放置一个断点,然后遍历代码。它在做你认为会怎么样的事情吗? – 2013-03-14 13:27:34

回答

0

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

看来CallToMethodWhichThrowsAnyException已经在处理CustomException从里面的某个地方抛出,这就是为什么你不抓住它。

此外,重新抛出的时候,你应该包装原始异常,使堆栈跟踪信息不会丢失:

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

我已经更新了我的问题,'CallToMethodWhichThrowsAnyException'确实不会自己处理异常。我真正的应用程序已经通过包装应用程序来保存堆栈跟踪,在我的问题中,它只是演示代码。 – 2013-03-14 13:32:40

2

第一次机会异常消息意味着正是它说,一个First chance exception

就你而言,它最有可能意味着你的调试器配置为不停止在这种类型的异常。由于这是自定义异常类型,因此这是默认行为。

要启用第一次机会异常中断,请转至Debug -> Exceptions并选择希望调试程序中断的异常类型。

+0

+ 1发布在msdn上的博客文章。但是,如果我理解正确,在上面的演示代码应该有第二个机会异常也被抛出,或不?因为我没有处理'CustomException' – 2013-03-14 13:43:18

+0

@FelixC - 这又取决于你的调试器配置 – TheBoyan 2013-03-14 13:46:36

相关问题