Dea*_*ean 0 c# breakpoints try-catch
如果我运行下面调用 Test1() 的代码,VS2012 将在该行很好地中断并准确显示发生了什么。如果我注释掉 Test1 并放入 Test2,那么 try catch 不会停止在线,它只是将其注销到控制台。
我怎样才能让 VS2012 停止在错误行上,即使它被 try catch 语句包围?
private void Button_Click(object sender, RoutedEventArgs e)
{
//Test1(); // No try catch - stops on error line dividing by zero.
Test2(); // Try catch - Just writes out to console.
}
private void MakeError()
{
int i = -1;
i += 1;
int j = 1 / i;
Console.WriteLine(j.ToString());
}
void Test1()
{
Console.WriteLine("MakeError in Test1");
MakeError();
}
void Test2()
{
Console.WriteLine("MakeError in Test2");
try
{
MakeError();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然其他答案是正确的,但这是默认情况下的有意行为,如果您愿意,您可以告诉 Visual Studio 提醒您注意这些异常。
在调试菜单上,单击例外。
在“异常”对话框中,为整个异常类别选择“抛出”,例如,公共语言运行时异常。
-或者-
展开异常类别的节点,例如,公共语言运行时异常,然后为该类别中的特定异常选择抛出。