【发布时间】:2017-02-27 17:57:12
【问题描述】:
一位同事在他的 VB.net 解决方案中发现了一个有线调试器行为。我承认这将是一个学术问题,因为这只会影响调试时突出显示的语句的顺序,而不影响代码的整体行为。所以对于所有好奇的人:
我们将其简化为以下最小控制台应用程序:
Private Sub PlayWithExceptions
Dim a = 2
Try
throw new Exception("1")
Catch ex As Exception
If a = 2 Then
Dim x = New XElement("Dummy")
Else
throw
End If
End Try
End Sub
Sub Main()
Try
PlayWithExceptions()
Catch ex As Exception
End Try
End Sub
很明显,调试器抛出 Exception(“1”) 并且调试器跳转到 PlayWithExceptions 方法的 catch 子句。在那里,因为“a”总是 2,调试器跳转到一些虚拟代码(New XElement…),从那里跳转到“End If”,最后回到 Else-leaf 到 throw 语句 .我承认 Visual Studio 不会重新抛出异常,但它看起来很奇怪。
将条件“If a = 2”更改为“If True”可以消除这种行为。
重构为条件捕获也消除了这种行为。
Private Sub PlayWithExceptions
Dim a = 2
Try
throw new Exception("1")
Catch ex As Exception When a = 2
Dim x = New XElement("Dummy")
Catch ex As Exception
throw
End Try
End sub
将这几行代码翻译成 C# 也不会显示这种行为。
private static void PlayWithExceptions()
{
var a = 2;
try
{
throw new Exception("1");
}
catch (Exception)
{
if (a == 2)
{
var x = new XElement("Dummy");
}
else
{
throw;
}
}
}
static void Main(string[] args)
{
try
{
PlayWithExceptions();
}
catch (Exception ex)
{
}
}
我们尝试了 .Net3.5 和 .Net4.6 以及目标 AnyCPU 和 x86,但对上述 VB 代码没有任何影响。代码使用默认调试设置执行,没有进一步优化。我们使用了 VS2015 Update 3。
有谁知道为什么 Visual Studio 会假装在 VB 中重新抛出异常(但没有真正重新抛出它)?调试时看起来很混乱……
【问题讨论】:
-
我想这只能由 vb.net/vs 调试器团队的人来回答
标签: c# vb.net debugging exception