/// <summary> /// This will terminate the app. /// Is there any difference between this and Exception_OnClick? /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MakeTeaAsyncWithExceptionAndAsyncVoid_OnClick(object sender, RoutedEventArgs e) { BeforeMakingTea(); try { _finalMessage = new StringBuilder(); _finalMessage.AppendLine("Making tea started."); MakeTeaAsyncWithException.MakeMeTeaAsyncVoidInTryCatch(); _finalMessage.AppendLine("After calling the method async"); Notes.Text = _finalMessage.ToString(); } catch (Exception exception) { Notes.Text = exception.Message; } AfterMakingTea(); }
/// <summary> /// Now, let's throw an exception inside an async method. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void MakeTeaAsyncWithException_OnClick(object sender, RoutedEventArgs e) { BeforeMakingTea(); try { _finalMessage = new StringBuilder(); _finalMessage.AppendLine("Making tea started."); _finalMessage.AppendLine(await MakeTeaAsyncWithException.MakeMeTeaAsync()); Notes.Text = _finalMessage.ToString(); } catch (Exception exception) { Notes.Text = exception.Message; } AfterMakingTea(); }
/// <summary> /// Does the async keyword affect the Exception? If we remove the async keyword from the async task that we are executing /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void MakeTeaAsyncWithExceptionWithoutAwait_OnClick(object sender, RoutedEventArgs e) { BeforeMakingTea(); try { _finalMessage = new StringBuilder(); _finalMessage.AppendLine("Making tea started."); // Will go inside the method. Will execute everything that has been established. When it reaches the await it will come back to the caller. MakeTeaAsyncWithException.MakeMeTeaAsyncInTryCatch(); _finalMessage.AppendLine("After calling the method async"); Notes.Text = _finalMessage.ToString(); } catch (Exception exception) { // Why we are not seeing the exception in the Notes? // If we don't use the await keyword, the exception will be swallowed. await keyword will validate that the task // completed successfully, but in this case, there's no await keyword. Moreover, there's no continuation and that's why we // can see that is not "async" and the next message is shown. Notes.Text = exception.Message; } AfterMakingTea(); }