Created
January 17, 2022 00:21
-
-
Save xiaomi7732/2544c9b8cb9a0d4d76a80425e5b04a4d to your computer and use it in GitHub Desktop.
Why async void is evil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async void AsyncJob() | |
| { | |
| await Task.Delay(100); | |
| throw new InvalidOperationException("Hello exception!"); | |
| // Console.WriteLine("Job is done!"); | |
| } | |
| try | |
| { | |
| // Notice: the exception thrown by AsyncJob won't be | |
| // caught by this try catch. | |
| // It will crash the process instead. | |
| AsyncJob(); | |
| } | |
| catch (Exception) | |
| { | |
| Console.WriteLine("Friendly error."); | |
| } | |
| Console.WriteLine("Hello from main!"); | |
| Console.ReadKey(intercept: true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment