是否有意义赶上ThreadAbortException和执行不采取行动? [英] Does it make sense to catch ThreadAbortException and perform no action?

查看:144
本文介绍了是否有意义赶上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)
{ }

或将导致该 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不能陷入完全;它会自动在块结束(见链接的MSDN文档页)的除非的<一个重新抛出href="http://msdn.microsoft.com/en-us/library/system.threading.thread.resetabort.aspx"><$c$c>Thread.ResetAbort被称为第一。

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屋!

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