on Clauses and Exception Types
Catch specific errors precisely.
Not All Errors Are Equal
A file error and a format error are different problems. Dart lets you catch them by type so each gets the right response. 🎯
The on Keyword
Use on to catch only one exception type. The block runs solely when that specific kind of error is thrown.
try {
parse();
} on FormatException {
print('Bad format');
}All lessons in this course
- throw, try, and catch
- on Clauses and Exception Types
- finally and Guaranteed Cleanup
- Custom Exceptions and rethrow