捕获 ThreadAbortException 且不执行任何操作是否有意义?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
无法捕获
ThreadAbortException
“完全地”;它将在catch
块的末尾自动重新抛出(请参阅链接的 MSDN 文档页面)除非Thread.ResetAbort
首先被调用。所以,唯一明智的
catch
块是:但这有一种非常邪恶的气味。可能没有理由这样做,因此您可能需要重新考虑您的方法。
更新:
SO 有很多涉及 Thread.Abort 的问题:
这个有相同的答案正如我在这里给出的。
这个有一个答案,扩展为“永远不要调用
Thread.Abort
,除非克苏鲁”正在上升”(我将其淡化为“邪恶的气味”)。还有很多其他的。
ThreadAbortException
cannot be caught "completely"; it will automatically be rethrown at the end of thecatch
block (see the linked MSDN docs page) unlessThread.ResetAbort
is called first.So, the only sensible
catch
block would be: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.
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.
在线程上调用 Thread.Abort 会有效地设置一个标志,每当代码未处理该异常或关联的
finallyThreadAbortException
代码>块。捕获异常而不调用 Thread.ResetAbort() 只会导致运行时在下次有机会时抛出另一个 ThreadAbortException。然而,这种行为并非完全没有意义,因为它会导致所有内部finally
块在外部异常过滤器块看到异常之前运行完成。这是否是一件好事将取决于应用程序。Calling
Thread.Abort
on a thread effectively sets a flag which will cause aThreadAbortException
to be thrown any time code isn't processing that exception nor associatedfinally
blocks. Catching the exception without callingThread.ResetAbort()
will simply result in the runtime throwing anotherThreadAbortException
at its next opportunity. Such behavior is not completely meaningless, however, since it will cause all innerfinally
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.如果您想针对不同类型的异常执行特定的操作,那么就需要有单独的 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
它会被抓住并丢失。您实际上应该只捕获可以执行某些操作或记录然后重新抛出的异常(使用 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];).