dotnet / dotnet/docs

Unhandled exceptions info not quite correct

Open
#27,267 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

File: the-managed-thread-pool.md
[https://docs.microsoft.com/en-us/dotnet/standard/threading/the-managed-thread-pool#exceptions-in-thread-pool-threads](https://docs.microsoft.com/en-us/dotnet/standard/threading/the-managed-thread-pool#exceptions-in-thread-pool-threads)

> Unhandled exceptions in thread pool threads terminate the process. There are three exceptions to this rule:
>
> A System.Threading.ThreadAbortException is thrown in a thread pool thread because Thread.Abort was called.
> A System.AppDomainUnloadedException is thrown in a thread pool thread because the application domain is being unloaded.
> The common language runtime or a host process terminates the thread.
> For more information, see Exceptions in Managed Threads.

These three exceptions to the rule come from a linked [page ](https://docs.microsoft.com/en-us/dotnet/standard/threading/exceptions-in-managed-threads) but misses a key fourth exception to the rule. In the linked document it also states:

> If any of these exceptions are unhandled in threads created by the common language runtime, the exception terminates the thread, but the common language runtime does not allow the exception to proceed further.

This second quote (also not quite correct) points to the fact that unhandled exceptions within Tasks running on the thread pool do NOT cause the process to terminate...unless they are async void. For example this .Net 6 console app:
```
// See https://aka.ms/new-console-template for more information
ThrowTaskExceptionAsync(); //this will NOT cause the application to crash even though the exception is unobserved
ThrowVoidExceptionAsync(); //this WILL cause the application to crash
Console.WriteLine("App still running, hit enter to continue...");
Console.ReadLine();

async void ThrowVoidExceptionAsync()
{
await Task.Delay(1000).ConfigureAwait(false);
throw new Exception();
}

async Task ThrowTaskExceptionAsync()
{
await Task.Delay(1000).ConfigureAwait(false);
throw new Exception();
}
```

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 2a04219c-aacc-199c-66b9-d3a394f5fac0
* Version Independent ID: 289ca45c-cdd4-94c6-56ec-922a587108b3
* Content: [The managed thread pool](https://docs.microsoft.com/en-us/dotnet/standard/threading/the-managed-thread-pool#exceptions-in-thread-pool-threads)
* Content Source: [docs/standard/threading/the-managed-thread-pool.md](https://github.com/dotnet/docs/blob/main/docs/standard/threading/the-managed-thread-pool.md)
* Product: **dotnet-fundamentals**
* GitHub Login: @BillWagner
* Microsoft Alias: **wiwagn**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.