大家应该都知道,.NET异常本质上是一个Object对象,也就是说只要执行newXXException()语句,就会分配到GCHeap中。这也意味着如果你有一个进程的转储文件,你可以从转储中导出程序最近抛出的异常。也就是说,只要这些异常没有被GC回收,就可以找出来。实现起来很简单,在windbg中输入如下命令即可。0:015>!dumpheap-typeException------------------------------Heap0AddressMTSize02ea6b0c79330a807202ea75f07930eab476…06f57aa47930eab47606f5829c7930eab47606f58a947930eab47606f5928c7930eab47606f59a847930eab47606f5a27c7930eab47606f5aa747930eab47606f5b26c7930eab47606f5ba647930eab47606f5c25c7930eab47606f5ca547930eab47606f5d24c7930eab476total319objects------------------------------total656objectsStatistics:MTCountTotalSizeClassName79333dc0112System.Text.DecoderExceptionFallback79333d7c112System.Text.EncoderExceptionFallback793172f8264System.UnhandledExceptionEventHandler79330c30172System.ExecutionEngineException79330ba0172System.StackOverflowException79330b10172System.OutOfMemoryException79330a80172System.Exception79330cc02144System.Threading.ThreadAbortException7930eab464649096System.IO.DirectoryNotFoundExceptionTotal656objects如果您想查看有关特定异常的详细信息,可以使用命令!pe02ea6b0c。!pe02ea6b0cExceptionobject:02ea6b0cExceptiontype:System.ExceptionMessage:TheemailenteredisnotavalidemailaddressInnerException:StackTrace(generated):SPIPFunction024AF2C80FE3125EApp_Code_da2s7oyo!BuggyMail.IsValidEmailAddress(System.String)+0x76024AF2E80FE31192App_Code_da2s7oyo!BuggyMail.SendEmail(System.String,System.String)+0x4aStackTraceString:HResult:80131500Therearenestedexceptionsonthisthread.Runwith-nestedfordetails现在问题来了,想查看所有异常的详细信息怎么办?用!pe命令把人肉一个个执行,会恶心死的。..所以友好的方式就是写脚本来提速,这里我使用.foreach命令。.foreach(ex{!dumpheap-typeException-short}){.echo"*************************************";!pe${ex}}上面我用了一个-short参数,目的是只输出address地址,方便脚本遍历,然后将迭代项发送给!pe,输出结果如下:0:015>.foreach(ex{!dumpheap-typeException-short}){.echo"***************************************";!pe${ex}}************************************异常对象:02ea6b0cExceptiontype:System.ExceptionMessage:TheemailenteredisnotavalidemailaddressInnerException:StackTrace(generated):SPIPFunction024AF2C80FE3125EApp_Code_da2s7oyo!BuggyMail.IsValidEmailAddress(System.String)+0x76024AF2E80FE31192App_Code_da2s7oyo!BuggyMail.SendEmail(System.String,System.String)+0x4aStackTraceString:HResult:80131500Thread.Runwith-nestedfordetails上有嵌套异常*******************************Exceptionobject:02ea75f0Exceptiontype:System.IO.DirectoryNotFoundExceptionMessage:Couldnotfinda路径的一部分'c:\idontexist\log.txt'.InnerException:<无>StackTrace(生成):SPIPFunction024AF044792741F2mscorlib_ni!System.IO.__Error.WinIOError(Int32,System.String)+0xc2024AF0A0792EB22Bmscorlib_ni!System.IO.FileStream.Init(System.String,System.IO.FileMode,System.IO.FileAccess,Int32,Boolean,System.IO.FileShare,Int32,System.IO.FileOptions,SECURITY_ATTRIBUTES,System.String,Boolean)+0x48b024AF198792EA882mscorlib_ni!System.IO.FileStream..ctor(System.String,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,Int32,System.IO.FileOptions)+0x42024AF1C07927783Fmscorlib_ni!System.IO.StreamWriter.CreateFile(System.String,Boolean)+0x3f024AF1D4792777DBmscorlib_ni!System.IO.StreamWriter..ctor(System.String,Boolean,System.Text.Encoding,Int32)+0x3b024AF1F4797EE19Fmscorlib_ni!System.IO.StreamWriter..ctor(System.String)+0x1f024AF2040FE31325App_Code_da2s7oyo!Utility.WriteToLog(System.String,System.String)+0x5dStackTraceString:HResult:80070003Therearenestedexceptionsonthisthread.Runwith-nestedfordetails***********************************异常对象:02ea7de8Exceptiontype:System.IO.DirectoryNotFoundExceptionMessage:Couldnotfindapartofthepath'c:\idontexist\log.txt'.InnerException:StackTrace(generated):SPIPFunction024AEF60792741F2mscorlib_ni!System.IO.__Error.WinIOError(Int32,System.String)+0xc2024AEFBC792EB22Bmscorlib_ni!System.IO.FileStream.Init(System.String,System.IO.FileMode,System.IO.FileAccess,Int32,Boolean,System.IO.FileShare,Int32,System.IO.FileOptions,SECURITY_ATTRIBUTES,System.String,Boolean)+0x48b024AF0B4792EA882mscorlib_ni!System.IO.FileStream..ctor(System.String,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,Int32,System.IO.FileOptions)+0x42024AF0DC7927783Fmscorlib_ni!System.IO.StreamWriter.CreateFile(System.String,Boolean)+0x3f024AF0F0792777DBmscorlib_ni!System.IO.StreamWriter..ctor(System.String,Boolean,System.Text.Encoding,Int32)+0x3b024AF110797EE19Fmscorlib_ni!System.IO.StreamWriter..ctor(System.String)+0x1f024AF1200FE31325App_Code_da2s7oyo!Utility.WriteToLog(System.String,System.String)+0x5dStackTraceString:HResult:80070003这个线程上有exceptions.Runwith-nestedfordetails当然也可以打印出当前异常的内部异常,加一个-nestparameter.foreach(ex{!dumpheap-typeException-short}){.echo"*****************************************";!pe–嵌套${ex}}