捕获 ThreadAbortException 且不执行任何操作是否有意义?

发布于 2024-10-11 15:08:40 字数 392 浏览 3 评论 0原文

catch (ThreadAbortException)
{ }
catch (Exception ex)
{
    TraceManager.TraceException(ex,
                                (int)ErrorCode.GENERIC_EXCEPTION,
                                ex.StackTrace + "\n" + ex.Message + "\n" + VendorUrl);
}

是否有意义?

catch (ThreadAbortException)
{ }

甚至拥有或将导致 ThreadAbortException 被吞噬并永远丢失

catch (ThreadAbortException)
{ }
catch (Exception ex)
{
    TraceManager.TraceException(ex,
                                (int)ErrorCode.GENERIC_EXCEPTION,
                                ex.StackTrace + "\n" + ex.Message + "\n" + VendorUrl);
}

does it make sense to even have the

catch (ThreadAbortException)
{ }

or will that cause the ThreadAbortException to be swallowed and lost forever?

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

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

发布评论

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

评论(5

征﹌骨岁月お 2024-10-18 15:08:40

无法捕获 ThreadAbortException “完全地”;它将在 catch 块的末尾自动重新抛出(请参阅链接的 MSDN 文档页面)除非 Thread.ResetAbort 首先被调用。

所以,唯一明智的 catch 块是:

catch (ThreadAbortException)
{
    // possibly do something here
    Thread.ResetAbort();
}

但这有一种非常邪恶的气味。可能没有理由这样做,因此您可能需要重新考虑您的方法。

更新:
SO 有很多涉及 Thread.Abort 的问题:

这个有相同的答案正如我在这里给出的。
这个有一个答案,扩展为“永远不要调用Thread.Abort,除非克苏鲁”正在上升”(我将其淡化为“邪恶的气味”)。

还有很多其他的。

ThreadAbortException cannot be caught "completely"; it will automatically be rethrown at the end of the catch block (see the linked MSDN docs page) unless Thread.ResetAbort is called first.

So, the only sensible catch block would be:

catch (ThreadAbortException)
{
    // possibly do something here
    Thread.ResetAbort();
}

But this has a really evil smell. There's probably no reason to do it, so you may want to rethink your approach.

Update:
There are many questions on SO that deal with Thread.Abort:

This one has the same answer as I have given here.
This one has an answer that expands on "don't ever call Thread.Abort unless Cthulhu is rising" (which I toned down considerably to an "evil smell").

There are also many others.

感悟人生的甜 2024-10-18 15:08:40

ThreadAbortException 不能这样被捕获。除非您调用 Thread.ResetAbort();,否则它将在 catch 块末尾自动重新抛出。

与此处的 ThreadAbortException 一样,拥有一个 catch 块可以自动重新抛出该异常,而无需 catch(Exception) 块尝试处理它。

The ThreadAbortException can't be caught like that. It will get rethrown automatically at the end of the catch block unless you call Thread.ResetAbort();

Having a catch block as you have here for ThreadAbortException allows it to be auto-rethrown without the catch(Exception) block attempting to handle it.

动听の歌 2024-10-18 15:08:40

在线程上调用 Thread.Abort 会有效地设置一个标志,每当代码未处理该异常或关联的finallyThreadAbortException代码>块。捕获异常而不调用 Thread.ResetAbort() 只会导致运行时在下次有机会时抛出另一个 ThreadAbortException。然而,这种行为并非完全没有意义,因为它会导致所有内部 finally 块在外部异常过滤器块看到异常之前运行完成。这是否是一件好事将取决于应用程序。

Calling Thread.Abort on a thread effectively sets a flag which will cause a ThreadAbortException to be thrown any time code isn't processing that exception nor associated finally blocks. Catching the exception without calling Thread.ResetAbort() will simply result in the runtime throwing another ThreadAbortException at its next opportunity. Such behavior is not completely meaningless, however, since it will cause all inner finally blocks to run to completion before the exception can be seen by outer exception-filter blocks. Whether or not that is a good thing will depend upon the application.

走过海棠暮 2024-10-18 15:08:40

如果您想针对不同类型的异常执行特定的操作,那么就需要有单独的 catch 块。否则你可以只使用一个异常捕获

If you want to do something specific for different kind of exceptions then it makes since to have seperate catch blocks. Otherwise you can just use the one Exception catch

沧桑㈠ 2024-10-18 15:08:40

它会被抓住并丢失。您实际上应该只捕获可以执行某些操作或记录然后重新抛出的异常(使用 throw; 而不是抛出 new [某些异常];)。

It will be caught and lost. You should really only be catching exceptions that you can do something with or that you log and then rethrow (with throw; not throw new [some exception];).

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