throw, try, and catch
Detect and respond to failures.
throw, try, and catch is a free Dart Academy lesson on CoddyKit — lesson 1 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.
When Things Go Wrong
Programs hit problems: bad input, missing files, broken math. Dart signals these with an exception instead of silently misbehaving. 🚨
Throwing an Exception
You raise a problem yourself with throw. It stops normal flow right where it happens and hands control off to a handler.
throw Exception('Something failed');You Can Throw Anything
Dart lets you throw almost any object, even a String. In practice you throw real Exception or Error objects so handlers stay clear.
throw FormatException('Bad number');Guarding Risky Code
Wrap code that might fail inside a try block. Dart runs it normally, but stands ready to react if anything throws.
try {
risky();
}Catching the Problem
A catch block runs only when the try block throws. It receives the thrown object so you can inspect and respond to it.
try {
risky();
} catch (e) {
print('Caught: $e');
}Flow Skips Ahead
When a throw fires, the rest of the try block is skipped instantly. Dart jumps straight to the matching catch block.
Reading the Error Object
The variable in catch (e) holds whatever was thrown. Printing it usually shows a helpful message describing what broke.
catch (e) {
print(e);
}Grabbing the Stack Trace
Add a second variable to catch to get the stack trace. It shows the call path that led to the failure for easier debugging.
catch (e, stack) {
print(stack);
}Recover, Do Not Crash
The whole point of catching is recovery. Instead of crashing, you can log, retry, or show a friendly fallback to the user.
Uncaught Means Crash
If nothing catches a thrown exception, it bubbles up and the program crashes with an error printed to the console.
A Complete Example
Here is the full pattern: a throw inside try, caught and handled gracefully so execution continues afterward.
try {
throw Exception('Oops');
} catch (e) {
print('Handled: $e');
}Quick Check
Which block actually responds to a thrown exception?
Recap
You now signal problems with throw, guard risky code in try, and recover in catch. That trio keeps your program alive. 🎉
Frequently asked questions
Is the “throw, try, and catch” lesson free?
Yes — the full text of “throw, try, and catch” 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 “throw, try, and catch”?
Detect and respond to failures. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “throw, try, and catch” 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.