本文介绍了捕获ThreadAbortException并执行任何操作是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

甚至有

  catch(ThreadAbortException)
{}

或将会导致 ThreadAbortException 被永久丢失并丢失

解决方案

不能被完全抓住;它将自动被重新抛出在 catch 块的末尾(请参阅链接的MSDN文档页面),除非 是首先调用



所以,唯一合理的 catch 块将是:

  catch(ThreadAbortException)
{
//可能在这里做某事
Thread.ResetAbort();
}

但这有一个非常恶毒的气味。可能没有理由这样做,所以你可能想重新考虑你的方法。



更新:
有很多问题这样处理 Thread.Abort



有与我在这里给出的相同的答案。
有一个答案,扩展了不要调用线程。中止除非Cthulhu正在上升(我把它大大降低为邪恶的气味)。



还有很多其他的。 p>

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?

解决方案

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.

这篇关于捕获ThreadAbortException并执行任何操作是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:05