确定代码是否从异常处理程序调用(using 语句)?

发布于 2024-09-12 01:57:17 字数 292 浏览 9 评论 0原文

我想做一些有点愚蠢的事情。在对象的 Dispose() 方法中,我想打印该对象的调试跟踪,告诉我该对象处于活动状态时发生的所有事件。

但由于这需要时间和金钱,因此我只想在因抛出异常而调用 Dispose() 时执行此操作。

所以我想做

if (exceptionIsCurrentlyRaised) PrintDebugStuff();

.NET是否有这样一个我可以查询的异常IsCurrentlyRaished属性?

I want to do something mildly silly. In my Dispose() method for an object, I want to print a debug trace for the object, telling me all events which happened while it was alive.

But as this takes time and money, I only want to do this if Dispose() is being called because an exception was thrown.

So I would like to do

if (exceptionIsCurrentlyRaised) PrintDebugStuff();

Does .NET have such a exceptionIsCurrentlyRaised property which I can query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾听心声的旋律 2024-09-19 01:57:17

我不知道是否存在这样的东西,因为我从未见过。但听起来您可以创建一个具有单个 bool 属性的接口。然后,当您位于 catch 语句内但在调用 dispose 方法之前,只需设置标志即可。

我猜这不可能是一个简单的解决方案,但我想我会开始一些想法。

编辑:好吧,我还发现这篇文章也有类似的问题: 判断是否因抛出异常而在finally块中执行

I don't know if anything like this exists as I have never seen it. But it sounds like you could just create an interface that has a single bool property. Then when you are inside the catch statement but before you call the dispose method just set the flag.

I'm guessing it can't be this easy of a solution but thought I would get some ideas started.

EDIT: Ok I also found this SO article that has a similar problem: Determine if executing in finally block due to exception being thrown

や三分注定 2024-09-19 01:57:17

有趣的问题,但我怀疑这是可能的 - 至少在没有使用调试或分析 API 的一些重大黑客攻击的情况下是不可能的。

即使您能够调用一些调试 API,这些 API 可以让您访问 catch 块内的当前异常,我认为您也无法在 finally 内获得异常块(这是执行 Dispose 方法的位置)。到那时,异常可能已经被处理,因此,就运行时而言,不存在异常。

我可以看到是否执行此操作的唯一方法是注册以获取自构造对象以来所有异常的通知,并从那里尝试确定异常是否被捕获。这个答案可能会有所帮助: .NET - First opportunity exception用于密集调试的侦听器?

Interesting question, but I doubt that this is possible - at least not without some major hacking using debugging or profiling APIs.

Even if you were able to call some debugging API that could give you access to the current exception inside a catch block I don't think you could get the exception inside a finally block (which is where your Dispose method would be executed). By then the exception may have been handled, so, as far as the runtime is concerned, there is no exception.

The only way I can see if doing this is to register to be notified of all exceptions since your object was constructed and from there try to figure out whether the exception was caught or not. This answer may be helpful: .NET - First chance exception listener for intensive debugging?

赠佳期 2024-09-19 01:57:17

实际上,这有点像 Visual Studio 2010 的“IntelliTrace”功能,它可以记录调试会话期间当您处于断点时发生的情况。

Actually, this is something like the "IntelliTrace" feature of Visual Studio 2010, which can record what happened during a debugging session when you were not at a breakpoint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文