Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Error Handling in Async Methods | Timeouts, Parallelism, and Error Handling
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Async and Await Practice

bookError Handling in Async Methods

When working with asynchronous methods in C#, error handling behaves differently from what you might expect in synchronous code. In a synchronous method, exceptions are thrown immediately when an error occurs. However, in async methods, exceptions are captured and stored in the returned Task. The exception is not thrown at the point where the error happens, but rather when you await the task. This difference is crucial for writing correct and robust error handling logic in your async programs.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using System; using System.Threading.Tasks; namespace ConsoleApp { public class Program { public static async Task Main(string[] args) { try { await PerformAsyncOperation(); } catch (Exception ex) { Console.WriteLine($"Caught exception in Main: {ex.Message}"); } } public static async Task PerformAsyncOperation() { try { await Task.Delay(500); throw new InvalidOperationException("Something went wrong in async operation!"); } catch (InvalidOperationException ex) { Console.WriteLine($"Handled in PerformAsyncOperation: {ex.Message}"); throw; // Re-throw to propagate to caller } } } }

When you await a task, any exception thrown inside the async method is re-thrown at the await point. This allows you to use familiar try/catch blocks around your await expressions. If you do not await the task, but instead access the Task.Exception property, you will find the exception wrapped inside an AggregateException. This is especially important when dealing with multiple tasks running in parallel. If you use Task.WhenAll to await several tasks at once, and more than one fails, all exceptions are collected and thrown together as an AggregateException. You must inspect the InnerExceptions property to see all the individual errors.

LoggingAndRecovery.cs

LoggingAndRecovery.cs

copy
123456789101112131415161718192021222324252627282930
using System; using System.Threading.Tasks; namespace ConsoleApp { public class LoggingAndRecovery { public async Task RunAsyncTasksWithLogging() { Task task1 = Task.Run(() => throw new InvalidOperationException("Task 1 failed")); Task task2 = Task.Run(() => throw new ArgumentException("Task 2 failed")); try { await Task.WhenAll(task1, task2); } catch (AggregateException ex) { foreach (var inner in ex.InnerExceptions) { // Log the error details Console.WriteLine($"Error: {inner.Message}"); } // Attempt recovery or fallback logic here Console.WriteLine("Attempting recovery after errors."); } } } }

In real-world applications, you should always log exceptions that occur in async code, especially when running multiple tasks in parallel. Use try/catch blocks around your awaited tasks, and always check for AggregateException when using Task.WhenAll or similar APIs. For critical operations, consider implementing retry logic, fallback strategies, or user notifications when errors are encountered. Robust error handling in async code is essential for building reliable, fault-tolerant applications.

Note
Definition

AggregateException is a special exception type in C# that represents one or more errors that occur during the execution of multiple tasks. When multiple tasks fail (such as with Task.WhenAll), their exceptions are combined into a single AggregateException, which contains a collection of all the individual exceptions in its InnerExceptions property.

1. How are exceptions propagated in async methods?

2. What happens if multiple tasks fail in Task.WhenAll?

3. What is the best way to handle exceptions in async code?

question mark

How are exceptions propagated in async methods?

Select the correct answer

question mark

What happens if multiple tasks fail in Task.WhenAll?

Select the correct answer

question mark

What is the best way to handle exceptions in async code?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you give an example of how to handle exceptions with async/await in C#?

What is the best way to log exceptions in async methods?

How do I implement retry logic for async operations?

bookError Handling in Async Methods

Deslize para mostrar o menu

When working with asynchronous methods in C#, error handling behaves differently from what you might expect in synchronous code. In a synchronous method, exceptions are thrown immediately when an error occurs. However, in async methods, exceptions are captured and stored in the returned Task. The exception is not thrown at the point where the error happens, but rather when you await the task. This difference is crucial for writing correct and robust error handling logic in your async programs.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using System; using System.Threading.Tasks; namespace ConsoleApp { public class Program { public static async Task Main(string[] args) { try { await PerformAsyncOperation(); } catch (Exception ex) { Console.WriteLine($"Caught exception in Main: {ex.Message}"); } } public static async Task PerformAsyncOperation() { try { await Task.Delay(500); throw new InvalidOperationException("Something went wrong in async operation!"); } catch (InvalidOperationException ex) { Console.WriteLine($"Handled in PerformAsyncOperation: {ex.Message}"); throw; // Re-throw to propagate to caller } } } }

When you await a task, any exception thrown inside the async method is re-thrown at the await point. This allows you to use familiar try/catch blocks around your await expressions. If you do not await the task, but instead access the Task.Exception property, you will find the exception wrapped inside an AggregateException. This is especially important when dealing with multiple tasks running in parallel. If you use Task.WhenAll to await several tasks at once, and more than one fails, all exceptions are collected and thrown together as an AggregateException. You must inspect the InnerExceptions property to see all the individual errors.

LoggingAndRecovery.cs

LoggingAndRecovery.cs

copy
123456789101112131415161718192021222324252627282930
using System; using System.Threading.Tasks; namespace ConsoleApp { public class LoggingAndRecovery { public async Task RunAsyncTasksWithLogging() { Task task1 = Task.Run(() => throw new InvalidOperationException("Task 1 failed")); Task task2 = Task.Run(() => throw new ArgumentException("Task 2 failed")); try { await Task.WhenAll(task1, task2); } catch (AggregateException ex) { foreach (var inner in ex.InnerExceptions) { // Log the error details Console.WriteLine($"Error: {inner.Message}"); } // Attempt recovery or fallback logic here Console.WriteLine("Attempting recovery after errors."); } } } }

In real-world applications, you should always log exceptions that occur in async code, especially when running multiple tasks in parallel. Use try/catch blocks around your awaited tasks, and always check for AggregateException when using Task.WhenAll or similar APIs. For critical operations, consider implementing retry logic, fallback strategies, or user notifications when errors are encountered. Robust error handling in async code is essential for building reliable, fault-tolerant applications.

Note
Definition

AggregateException is a special exception type in C# that represents one or more errors that occur during the execution of multiple tasks. When multiple tasks fail (such as with Task.WhenAll), their exceptions are combined into a single AggregateException, which contains a collection of all the individual exceptions in its InnerExceptions property.

1. How are exceptions propagated in async methods?

2. What happens if multiple tasks fail in Task.WhenAll?

3. What is the best way to handle exceptions in async code?

question mark

How are exceptions propagated in async methods?

Select the correct answer

question mark

What happens if multiple tasks fail in Task.WhenAll?

Select the correct answer

question mark

What is the best way to handle exceptions in async code?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5
some-alt