考虑这个程序:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication20
{
class Program
{
static void Main( string[] args )
{
DoSomething() ;
return ;
}
static void DoSomething()
{
try
{
DoSomethingThatFails() ;
}
catch( Exception e )
{
throw new InvalidOperationException( "This is the wrapper exception" , e ) ;
}
}
static int DoSomethingThatFails()
{
int x = 3 ;
int y = 0 ;
int z = x / y ; // can you say "divide by zero"?
return z ;
}
}
}
这是写入控制台的内容:
Unhandled Exception: System.InvalidOperationException: This is the wrapper exception ---> System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication20.Program.DoSomethingThatFails() in ...\Program.cs:line 32
at ConsoleApplication20.Program.DoSomething() in ...\Program.cs:line 21
--- End of inner exception stack trace ---
at ConsoleApplication20.Program.DoSomething() in ...\Program.cs:line 25
at ConsoleApplication20.Program.Main(String[] args) in ...\Program.cs:line 14
您会注意到第一行包含外部异常及其消息(InvalidOperationException
和This is the wrapper exception
),然后是箭头(--->
),然后是内部异常(DivideByZeroException
和Attempted to divide by zero.
)。
接下来的两行是内部异常的堆栈跟踪,后面跟着一个标记来指示内部异常的堆栈跟踪的结束。然后你会得到外部异常的堆栈跟踪。
所有的信息都在那里,你只是没有看到它。