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
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.