Why is the CLR rethrowing a ThreadAbortException?

I got the following code from the book "Parallel Programming in Windows":

void Main() { try { try { Console.WriteLine("Inside Main Method"); Thread.CurrentThread.Abort(); } catch(ThreadAbortException) { Console.WriteLine("Inside First Catch"); // Trying to swallow but CLR throws it again.... } } catch(ThreadAbortException) { Console.WriteLine("Inside Second Catch"); //Thread.ResetAbort(); } } 

I am interested to know why the CLR throws a ThreadAbortException? And he continues to do this until he calls "Thread.ResetAbort ()." Secondly, is there any other systemic exception that receives special treatment from CLR?

+6
source share
2 answers

I am interested to know why the CLR throws a ThreadAbortException?

Because the flow is interrupted. People handle all exceptions all the time, although this is dangerous. It would be strange if the error logging program, say, retained a stream that was supposed to be destroyed forever, no?

Is there any other system exception that receives special handling from the CLR?

Yes, there are several. For example, stacks and exceptions from memory also have special behavior.

+15
source

This is a special exception, http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx , see notes. In my opinion, the reason is that .Net gives you the ability to do any cleanup before the stream closes.

See a little about plumbing: http://ondotnet.com/pub/a/dotnet/2003/02/18/threadabort.html

+3
source

Source: https://habr.com/ru/post/902173/


All Articles