Chapter 13 — Exceptions and Error Handling
Part 1A — Learning Dart · Chapter 13 of 62 · 5 h
Chapter 13 — Exceptions and Error Handling
Level: beginner / intermediate Estimated time: 5 h Prerequisites: Chapter 12 — Null safety What you will be able to do by the end: write a Dart program that never crashes abruptly on invalid data, by signaling problems with
throwand catching them in the right place withtry/on/catch/finally.
13.0 — Chapter Objectives
By the end of this chapter, you will be able to:
- explain, step by step, what happens when a Dart program crashes;
- distinguish an
Error(a programmer's fault) from anException(a foreseeable accident); - read a Dart VM error trace without panicking;
- protect a piece of code with
try; - catch a problem with
catch; - retrieve the thrown object with
catch (e)and the call stack withcatch (e, s); - filter a specific exception type with
on; - chain several
onclauses in the right order; - combine
onandcatchwithon ... catch (e); - guarantee that cleanup code runs with
finally; - describe precisely the execution order of
try,catch, andfinally; - signal a problem yourself with
throw; - propagate an already-caught exception with
rethrow; - create your own domain-specific exception classes for your game;
- understand and trigger a
FormatException; - explain why
int.parse()fails and how its message is built; - replace an exception with a check using
int.tryParse()anddouble.tryParse(); - validate a complete user input, from raw text to a usable value;
- use
ArgumentErrorandassertto protect your own functions; - combine exceptions and null safety;
- decide, in a real project, when to catch an exception and when to let the program crash.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion