4 ms·
"Async void methods will crash the process if an exception is thrown" This is news to me. AFAIK one of the problems is that exceptions in that method will go u
by bertylicious 2y ago
"Async void methods will crash the process if an exception is thrown"
This is news to me. AFAIK one of the problems is that exceptions in that method will go unnoticed because they never reach the root thread when they occur. They might only become visible, wrapped in an UnobservedTaskException, when the process shuts down.
Edit: Maybe the author meant that the unhandled exception will cause an invalid system state and thus cause other exceptions? Or maybe I'm just completely wrong here.
- tyleo 2y agoI believe that ‘crash the runtime’ is legitimate old behavior. IIRC changes eventually made the default crash on UnobservedTaskException an optional configuration setting and there is now an event they are reported with.
- Netcob 2y agoThat's my understanding too. I usually add an event handler to TaskScheduler.UnobservedTaskException that logs any exception like that so at least there's some trace of it. You could also use it to end the process with an error this way if you prefer to treat uncaught exceptions as fatal errors.
- deleted 2y ago[deleted]
- gwbas1c 2y agoI always thought that "async void" methods existed so async methods could be sent to event handlers; AND, I've always believed event handlers should have a top-level try/catch. (I personally can't remember using "async void;" seems like something like a goto that technically has a valid use case, but it's simpler to avoid.)