0PricingLogin
Dart Academy · Lesson

Error Handling in async Code

Use try/catch with await safely.

Futures Can Fail

Async work can go wrong: a server times out or a file is missing. A Future can complete with an error instead of a value.

try and catch With await

Wrap an await in try/catch and a failed future throws right at that line, just like ordinary synchronous errors.

try {
  final data = await fetch();
} catch (e) {
  print("failed: $e");
}

All lessons in this course

  1. The Event Loop and Microtasks
  2. Creating and Awaiting Futures
  3. Error Handling in async Code
  4. Future.wait and Parallel Work
← Back to Dart Academy