Does it make sense to catch ThreadAbortException and perform no action?

后端 未结 5 2277
旧时难觅i
旧时难觅i 2021-02-20 01:31
catch (ThreadAbortException)
{ }
catch (Exception ex)
{
    TraceManager.TraceException(ex,
                                (int)ErrorCode.GENERIC_EXCEPTION,
                    


        
5条回答
  •  [愿得一人]
    2021-02-20 02:03

    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.

提交回复
热议问题