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
- The Event Loop and Microtasks
- Creating and Awaiting Futures
- Error Handling in async Code
- Future.wait and Parallel Work