Skip to content
Justice Tech School

throw and Custom Exceptions

throw raises any object. Implement Exception for your own types.

Example
class AgeError implements Exception {
  final String msg;
  AgeError(this.msg);
  @override String toString() => "AgeError: $msg";
}
void check(int a) { if (a < 0) throw AgeError("negative"); }
void main() {
  try { check(-1); } catch (e) { print(e); }
}

Exercise

Raise.

 AgeError("x");

Quiz Test

What should custom exceptions implement?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.