none
Another Thread.Abort question.... RRS feed

  • Question

  • Hello Everybody,

    I have another one of the infamous Thread.Abort questions.

    1)I do know that the ThreadAbortedException is retherown after catch/finally, however, is that true for nested try/catch/finallys, too, or do I have to manually rethrow it after the first level if I want to traverse out so it goes out to the top most function/try/catch.

    [Before comments come up that I shouldn't use Abort: I have already a cancellation mechanism in place, just if that doesn't give the desired results after a certain amount of time I resort to Abort())]

    2)If you (re)throw a different exception in a catch block that was entered due to a ThreadAbortException, does that override the automatic rethrow by the CLR or would it ignore my exception and just rethrow its ThreadAbortedException? (I am using the catches to log vital information)

    Thank you for your help!

    Best regards,

    Frank

    Friday, July 13, 2012 4:13 PM

Answers

  • Hi T3quila,

    Welcome to the MSDN Forum.

    >>1

    You don't need to re-throw it. 

    Please check this code snippet: the outer try-catch always catches the threadAbortException:

        Public Sub main()
            For i = 0 To 1000
                Console.WriteLine(i)
            Next
            Try
                Try
                    Threading.Thread.CurrentThread.Abort()
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                'Threading.Thread.ResetAbort()
                Console.ReadLine()
            End Try
            Console.ReadLine()
        End Sub

    >>2

    No, the new exception doesn't override the threadAbortedException.

        Public Sub main()
            For i = 0 To 1000
                Console.WriteLine(i)
            Next
            Try
                Try
                    Threading.Thread.CurrentThread.Abort()
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
                Throw New Exception
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.ReadLine()
            End Try
            Console.ReadLine()
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by T3quila Monday, July 16, 2012 12:50 PM
    Monday, July 16, 2012 8:11 AM
    Moderator

All replies

  • Hi T3quila,

    Welcome to the MSDN Forum.

    >>1

    You don't need to re-throw it. 

    Please check this code snippet: the outer try-catch always catches the threadAbortException:

        Public Sub main()
            For i = 0 To 1000
                Console.WriteLine(i)
            Next
            Try
                Try
                    Threading.Thread.CurrentThread.Abort()
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                'Threading.Thread.ResetAbort()
                Console.ReadLine()
            End Try
            Console.ReadLine()
        End Sub

    >>2

    No, the new exception doesn't override the threadAbortedException.

        Public Sub main()
            For i = 0 To 1000
                Console.WriteLine(i)
            Next
            Try
                Try
                    Threading.Thread.CurrentThread.Abort()
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
                Throw New Exception
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.ReadLine()
            End Try
            Console.ReadLine()
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by T3quila Monday, July 16, 2012 12:50 PM
    Monday, July 16, 2012 8:11 AM
    Moderator
  • Hi Mike,

    thank you very much for the answer!

    Best regards,

    Frank

    Monday, July 16, 2012 12:50 PM