0Pricing
Dart Academy · Lesson

Custom Exceptions and rethrow

Define meaningful errors and re-raise them.

Custom Exceptions and rethrow is a free Dart Academy lesson on CoddyKit — lesson 4 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.

Errors That Mean Something

Generic exceptions hide intent. A custom exception names the exact problem, making failures readable across your whole app. 🏷️

Implementing Exception

Create a class that implements Exception. That marks it as a throwable error type Dart and readers recognize instantly.

class PaymentException implements Exception {}

Carrying a Message

Give your exception a message field through its constructor so handlers can read why it happened.

class PaymentException implements Exception {
  final String message;
  PaymentException(this.message);
}

A Friendly toString

Override toString so printing the exception shows a clear sentence instead of just the class name.

@override
String toString() => 'PaymentException: $message';

Throwing Your Own

Throw a custom exception exactly like a built-in one. Use throw with a fresh instance when a rule is broken.

throw PaymentException('Card declined');

Catching by Your Type

Catch your exception with an on clause, just like the built-in types, to handle that case specifically.

try {
  pay();
} on PaymentException catch (e) {
  print(e);
}

Sometimes You Cannot Fix It

A handler might log an error but not be able to resolve it. Instead of swallowing it, you can pass it onward with rethrow.

The rethrow Keyword

rethrow throws the current exception again, preserving its original stack trace so callers see where it truly began.

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

rethrow Beats throw e

Writing throw e resets the stack trace; rethrow keeps it intact. Always prefer rethrow when re-raising inside a catch.

Log Then Pass Up

A common pattern: catch to log the failure, then rethrow so a higher layer decides how to recover.

catch (e) {
  logger.error(e);
  rethrow;
}

Adding Useful Fields

Custom exceptions can hold extra data like a code or failed value, giving handlers richer context to act on.

class ApiException implements Exception {
  final int code;
  ApiException(this.code);
}

Quick Check

Why use rethrow instead of throw e inside a catch?

Recap

You can now build meaningful exceptions that implement Exception, carry data, and use rethrow to pass failures up cleanly. 🚀

Frequently asked questions

Is the “Custom Exceptions and rethrow” lesson free?

Yes — the full text of “Custom Exceptions and rethrow” 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 “Custom Exceptions and rethrow”?

Define meaningful errors and re-raise them. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Exceptions and rethrow” 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