Rethrown Exception的原始位置 [英] Rethrown Exception's Original Location

查看:247
本文介绍了Rethrown Exception的原始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在读一本名为CLR via C#的书。在Exceptions部分,我偶然发现了这部分我无法理解。显然下面的代码是一些程序员如何处理未处理的异常中丢失的原始异常点的问题:



Hello. I'm reading through the book called "CLR via C#". In Exceptions part, i have stumbled into this part i can't understand. Apparently the code below is how some programmers handle the issue of lost original exception point in unhandled exceptions:

private void SomeMethod() {
 Boolean trySucceeds = false;
 try {
 ...
 trySucceeds = true;
 }
 finally {
 if (!trySucceeds) { /* catch code goes in here */ }
 }
}





我知道在未处理的异常弹出之前,我终于先完成阻止,但我不明白它是如何完成保留原始异常点的。任何想法?



提前谢谢。



I know finally block finishes first before Unhandled Exception pops up, but i don't understand how does it accomplish preserving original exception point. Any ideas?

Thanks in advance.

推荐答案

如果我正确地回忆起这本书,那么代码是应该以在Windows上实现CLR的方式解决问题。即使你以这种方式重新抛出异常,CLR也会知道完整的堆栈跟踪;

If I recall the book correctly, that code is supposed to work around an issue in the way the CLR is implemented on Windows. Even though the CLR will know the full stack trace if you rethrow an exception this way;
void SomeMethod() {
  try {
     // Do something that may throw
  }
  catch {
     // caught it, do something and then re-throw
     throw;
  ]
}



Windows将从重新抛出点创建一个新的堆栈帧。如果您捕获并记录异常,这不是问题,因为这将为您提供正确的堆栈跟踪,但如果它崩溃了应用程序(因为它未被处理)并且您需要使用类似 windbg的东西检查故障转储,异常的原始起点将丢失。



在大多数情况下,你不需要担心这个,它经常更好的方法是添加一个未捕获的异常处理程序来记录异常及其堆栈跟踪,这比使用 windbg (或者至少通常是)更容易用作故障排除的基础。



希望这会有所帮助,

Fredrik


Windows will create a new stack frame from the re throw point. This isn't an issue if you catch and log the exception, as that will give you the correct stack trace, but if it crashes the application (because it's un-handled) and you need to use something like windbg to inspect the crash dump, the original originating point of the exception will be lost.

In most cases you don't need to worry about this, it's often better just to add an uncaught exception handler that logs the exception and it's stack trace, that's easier to use as a basis for trouble shooting than windbg (or at least, it often is).

Hope this helps,
Fredrik


这篇关于Rethrown Exception的原始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆