在switch语句中抛出异常的堆栈跟踪中行号错误
本文关键字:跟踪 错误 堆栈 switch 语句 抛出异常 | 更新日期: 2023-09-27 18:14:36
我注意到异常堆栈跟踪中的行号有一个奇怪的行为,如果异常是在switch
语句中抛出的。
下面是一个例子(由于行号的关系,格式当然很重要):
using System;
class Program {
static void Main(string[] args) {
for (int i = 0; i < 3; i++) {
try {
ThrowSomeException(i);
} catch (Exception exc) {
Console.WriteLine(exc);
}
}
}
private static void ThrowSomeException(int arg) {
Console.WriteLine("arg = {0}", arg);
switch (arg) {
case 0:
throw new Exception("Line number = 16");
case 1:
throw new Exception("Line number = 18");
default:
throw new Exception("Line number = 20");
}
}
}
堆栈跟踪中报告的行是switch语句中下一个异常所在的行。上面的程序产生如下结果(注意异常文本中的行号与堆栈跟踪中的行号不匹配):
arg = 0
System.Exception: Line number = 16
at Program.ThrowSomeException(Int32 arg) in x:'test'Program.cs:line 18
at Program.Main(String[] args) in x:'test'Program.cs:line 6
arg = 1
System.Exception: Line number = 18
at Program.ThrowSomeException(Int32 arg) in x:'test'Program.cs:line 20
at Program.Main(String[] args) in x:'test'Program.cs:line 6
arg = 2
System.Exception: Line number = 20
at Program.ThrowSomeException(Int32 arg) in x:'test'Program.cs:line 20
at Program.Main(String[] args) in x:'test'Program.cs:line 6
为什么会发生这种情况?
注意:我在VS 2012和2013上都试过了,在。net 3.5和4.5上编译,得到了相同的结果。
Debug vs Release:令人惊讶的是,我只在Debug中得到这个奇怪的行为,在Release中行号是正确的。
我试过反编译程序集,似乎程序集在两种模式下都做了一些优化。特别是第二个方法被改变了:
private static void ThrowSomeException(int arg)
{
Console.WriteLine("arg = {0}", arg);
switch (arg)
{
case 0:
{
throw new Exception("Line number = 16");
}
case 1:
{
throw new Exception("Line number = 18");
}
}
throw new Exception("Line number = 20");
}
这是Telerik JustDecompile告诉我的方法在调试和发布模式下的样子。如果您查看原始程序集,很可能会有关于为什么存在这种差异的进一步解释。
我不知道如何继续,但我认为这是一个非常有趣的问题。我将把我的答案标记为Community Wiki,希望大家共同努力可以解决这个问题。
我又做了一些测试。我将ThrowSomeException()函数移动到一个单独的类中,并使其成为非静态的,这并没有改变任何东西。然后我稍微重写了一下,首先将异常赋值给一个变量,然后单独抛出它。
internal class Program
{
private static void Main()
{
Test test = new Test();
for (int i = 0; i < 3; i++)
{
try
{
test.ThrowSomeException(i);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
}
public class Test
{
public void ThrowSomeException(int arg)
{
Console.WriteLine("arg = {0}", arg);
switch (arg)
{
case 0:
{
Exception ex = new Exception("Line number = 36");
throw ex;
}
case 1:
{
Exception ex = new Exception("Line number = 41");
throw ex;
}
default:
{
Exception ex = new Exception("Line number = 46");
throw ex;
}
}
}
}
以上代码在调试模式下有如下输出:
arg = 0
System.Exception: Line number = 36
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 40
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
arg = 1
System.Exception: Line number = 41
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 45
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
arg = 2
System.Exception: Line number = 46
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 47
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
在Release模式下,第二个异常从第46行而不是第45行抛出。这种行为对于所有版本的。net框架和所有版本的vs都是一致的。我接下来将尝试使用VB项目来做这件事,看看它是否有区别。
编辑:使用以下VB项目在VS 2012中:
Module Program
Sub Main()
Dim test As New Test()
For i As Integer = 0 To 2
Try
test.ThrowSomeException(i)
Catch exception As Exception
Console.WriteLine(exception)
End Try
Next
End Sub
End Module
Public Class Test
Public Sub ThrowSomeException(arg As Integer)
Console.WriteLine("arg = {0}", arg)
Select Case arg
Case 0
If True Then
Dim ex As New Exception("Line number = 22")
Throw ex
End If
Case 1
If True Then
Dim ex As New Exception("Line number = 27")
Throw ex
End If
Case Else
If True Then
Dim ex As New Exception("Line number = 32")
Throw ex
End If
End Select
End Sub
End Class
问题没有发生,行号一致。
我还测试了直接由输出生成的可执行文件,并发现了更奇怪的结果。这是调试exe的输出:
arg = 0
System.Exception: Line number = 36
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 40
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
arg = 1
System.Exception: Line number = 41
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 45
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
arg = 2
System.Exception: Line number = 46
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 47
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
这是释放模式exe的输出:
arg = 0
System.Exception: Line number = 36
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 41
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
arg = 1
System.Exception: Line number = 41
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 42
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
arg = 2
System.Exception: Line number = 46
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 37
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 14
这与使用调试器运行此命令的结果不同。
快速测试似乎也表明异常和抛出语句之间的额外行(在这种情况下是console.writeline())也会影响结果:
重写为:
switch (arg)
{
case 0:
{
Exception ex = new Exception("Line number = 37");
Console.WriteLine("case 0");
throw ex;
}
case 1:
{
Exception ex = new Exception("Line number = 43");
Console.WriteLine("case 1");
throw ex;
}
default:
{
Exception ex = new Exception("Line number = 49");
Console.WriteLine("case default");
throw ex;
}
}
给出了VS的Release输出:
arg = 0
case 0
System.Exception: Line number = 37
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 43
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 1
case 1
System.Exception: Line number = 43
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 49
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 2
case default
System.Exception: Line number = 49
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 51
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
和命令行输出:
arg = 0
case 0
System.Exception: Line number = 37
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 43
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 1
case 1
System.Exception: Line number = 43
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 45
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 2
case default
System.Exception: Line number = 49
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 39
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
和VS的Debug输出:
arg = 0
case 0
System.Exception: Line number = 37
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 42
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 1
case 1
System.Exception: Line number = 43
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 48
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 2
case default
System.Exception: Line number = 49
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 51
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
命令行输出如下:
arg = 0
case 0
System.Exception: Line number = 37
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 42
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 1
case 1
System.Exception: Line number = 43
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 48
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15
arg = 2
case default
System.Exception: Line number = 49
bij linenumbertest.Test.ThrowSomeException(Int32 arg) in c:'Users'nate'Docume
nts'Visual Studio 2012'Projects'linenumbertest'Program.cs:regel 51
bij linenumbertest.Program.Main() in c:'Users'nate'Documents'Visual Studio 20
12'Projects'linenumbertest'Program.cs:regel 15