0Pricing
Dart Academy · Lesson

Error Handling in async Code

Use try/catch with await safely.

Error Handling in async Code is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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");
}

Catching Specific Types

Use an on clause to handle one error kind precisely while letting other failures bubble up untouched.

try {
  await load();
} on TimeoutException {
  print("too slow");
}

Inspecting the Error

Add catch (e, s) to grab both the error object and its stack trace, which makes debugging async failures far easier.

catch (e, s) {
  print(e);
  print(s);
}

finally Always Runs

A finally block runs whether the await succeeded or threw, so it is the right place to close files or stop spinners.

try {
  await save();
} finally {
  closeFile();
}

Throwing From async

Throw inside an async function and the returned future simply completes with that error, ready for a caller to catch.

Future<int> risky() async {
  throw StateError("nope");
}

Errors in then Chains

Without await, attach catchError to a future to handle a failure in a then-style chain.

fetch().then(use).catchError(handle);

Recovering With a Default

In catchError or catch you can return a fallback value, letting your program continue gracefully after a failure.

Unhandled Future Errors

An ignored failed future becomes an uncaught error that may crash or log loudly. Always catch or await your futures.

Rethrowing When Needed

Use rethrow inside a catch to log a failure here yet still let an outer handler deal with it too.

catch (e) {
  log(e);
  rethrow;
}

await Inside finally

You can even await inside finally to perform async cleanup, like flushing a buffer, before the function returns.

Quick Check

Quick check on catching async errors.

Recap: Async Errors

You learned to guard await with try/catch, target types with on, clean up in finally, and recover or rethrow. 🛡️

Frequently asked questions

Is the “Error Handling in async Code” lesson free?

Yes — the full text of “Error Handling in async Code” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Error Handling in async Code”?

Use try/catch with await safely. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Error Handling in async Code” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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