0Pricing
Dart Academy · Lesson

on Clauses and Exception Types

Catch specific errors precisely.

on Clauses and Exception Types is a free Dart Academy lesson on CoddyKit — lesson 2 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.

Not All Errors Are Equal

A file error and a format error are different problems. Dart lets you catch them by type so each gets the right response. 🎯

The on Keyword

Use on to catch only one exception type. The block runs solely when that specific kind of error is thrown.

try {
  parse();
} on FormatException {
  print('Bad format');
}

Keeping the Object Too

Combine on with catch to filter by type and still grab the error object for its message details.

on FormatException catch (e) {
  print(e.message);
}

Order Matters

Dart checks on clauses top to bottom and uses the first match. Put specific types before general ones, or specific ones never run.

Multiple on Clauses

Stack several on clauses on one try. Each handles a different exception type with its own tailored logic.

try {
  work();
} on FormatException {
} on RangeError {
}

A Catch-All at the End

A plain catch with no type catches anything left over. Place it last as a safety net after typed clauses.

} on FormatException {
} catch (e) {
  print('Other: $e');
}

Exception vs Error

Dart splits failures into Exception for recoverable issues and Error for programming bugs you should fix, not catch.

Common Built-in Types

You will meet FormatException, RangeError, StateError, and ArgumentError. Knowing them lets you catch precisely.

Catching a Supertype

An on Exception clause matches any subtype of Exception. Use a broad type when several related errors share one handler.

try {
  go();
} on Exception catch (e) {
  log(e);
}

Why Type Filtering Helps

Typed handlers keep code clear: you react to a missing file differently than to bad input, instead of one giant catch guessing.

Putting It Together

This shows specific-to-general flow: a typed on clause first, then a catch-all to handle everything else safely.

try {
  run();
} on FormatException catch (e) {
  print(e);
} catch (e) {
  print('Unknown');
}

Quick Check

How do you catch only one specific exception type?

Recap

You can now target errors with on, order clauses specific-first, and add a catch-all net. Precise handling, clean code. ✅

Frequently asked questions

Is the “on Clauses and Exception Types” lesson free?

Yes — the full text of “on Clauses and Exception Types” 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 “on Clauses and Exception Types”?

Catch specific errors precisely. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “on Clauses and Exception Types” 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. throw, try, and catch
  2. on Clauses and Exception Types
  3. finally and Guaranteed Cleanup
  4. Custom Exceptions and rethrow
← Back to Dart Academy