Recall that Richter wrote that UnhandledException interrupts the thread in which it occurred. I tried to apply it for the experiment. I wrote this code here:

 class Program { static void ThrowException(object obj) { try { throw new Exception(); Console.Write("1");//Я знаю, что компилятор опустит эту строчку :D } catch { Console.WriteLine("--------------Exception was throwen."); throw; } } static void Main(string[] args) { Console.WriteLine("Are you ready for Tasks?"); Task.Run(() => ThrowException(null)); Thread.Sleep(1000); Console.WriteLine("Tasks OK"); Thread.Sleep(3000); Console.WriteLine("Are you ready for Threads?"); Thread thread = new Thread(() => ThrowException(null)); thread.Start(); Thread.Sleep(1000); Console.WriteLine("Threads OK"); Thread.Sleep(3000); Console.WriteLine("Are you ready for ThreadPool?"); ThreadPool.QueueUserWorkItem(ThrowException); Thread.Sleep(1000); Console.WriteLine("ThreadPool OK"); Console.ReadKey(true); } } 

With Task everything works as intended, but after this "magic" with Thread application stops. I googled and still do not understand, because in Microsoft Docs written

It is not clear that there has been a situation in the process of extracting.

Why is the application closing?

  • launched. Nothing stops at me, the program works to the end. - tym32167
  • @ tym32167, it becomes more interesting. I have it flies around 39 lines. And on which .NET Framework program was launched? I have 4.6.1 - Kamushek
  • The 39th line is Thread.Sleep(1000); What can fly there? I ran the linkup, the framework I have is 4.5.1 - tym32167
  • @ tym32167, around this line, just the first thread has time to reach this line sometimes, while the exception is thrown. I have no idea why the program is interrupted, although the logic should be as you have. - Kamushek

1 answer 1

Unhandled exception interrupts code execution throughout the App Domain . In the case of Task.Run , the exception is not unhandled, you can see for yourself by changing the code:

 Console.WriteLine("Are you ready for Tasks?"); var result = Task.Run(() => ThrowException(null)); Thread.Sleep(1000); Console.WriteLine("Tasks OK"); Console.WriteLine("Exception: " + result.Exception); // здесь AggregateException Thread.Sleep(3000); 

You will see an instance of AggregateException , and inside it is your original Exception . This means that this exception was intercepted, which means that it did not put the domain.

In the case of Thread or ThreadPool , no one catches an exception, which means it crashes the application domain.

In your quote from Microsoft Docs, there are only 3 types of exceptions: ThreadAbortException , AppDomainUnloadedException , and some internal ones. For example, if you call Thread.Abort() , then even if no one catches this exception, the application domain does not crash (otherwise what's the point of interrupting the flow). This rule does not apply to all others.

  • But how then did the @ tym32167 code run to the end? - Kamushek
  • seems to be true. Lincupad itself is possible somewhere that something intercepts, because it does not crash. The copied file hangs for some time, then it falls. - tym32167
  • @Zergatul, Thanks, now I know more: D - Kamushek