0%
Exceptions and Error Handling — Chapter 13

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 throw and catching them in the right place with try / 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 an Exception (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 with catch (e, s);
  • filter a specific exception type with on;
  • chain several on clauses in the right order;
  • combine on and catch with on ... catch (e);
  • guarantee that cleanup code runs with finally;
  • describe precisely the execution order of try, catch, and finally;
  • 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() and double.tryParse();
  • validate a complete user input, from raw text to a usable value;
  • use ArgumentError and assert to 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

Link copied!