是否有意义赶上ThreadAbortException和执行不采取行动? [英] Does it make sense to catch ThreadAbortException and perform no action?
问题描述
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)
{ }
或将导致该 ThreadAbortException
被吞噬,并永远失去了?
or will that cause the ThreadAbortException
to be swallowed and lost forever?
推荐答案
<一个href="http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx"><$c$c>ThreadAbortException$c$c>不能陷入完全;它会自动在抓
块结束(见链接的MSDN文档页)的除非的<一个重新抛出href="http://msdn.microsoft.com/en-us/library/system.threading.thread.resetabort.aspx"><$c$c>Thread.ResetAbort$c$c>被称为第一。
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.
更新:
有许多问题上,这样处理 Thread.Abort的
:
Update:
There are many questions on SO that deal with Thread.Abort
:
这其中有相同的答案,因为我在这里给出。
这其中具有扩展了永远不要叫一个答案 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").
也有许多其他问题。
这篇关于是否有意义赶上ThreadAbortException和执行不采取行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!