Skip to content

Instantly share code, notes, and snippets.

@hawkkiller
Last active January 21, 2024 15:14
Show Gist options
  • Save hawkkiller/14dccb4e2335176fdebd328d1c723808 to your computer and use it in GitHub Desktop.
Save hawkkiller/14dccb4e2335176fdebd328d1c723808 to your computer and use it in GitHub Desktop.
Error-handling example #1
void main() {
try {
sendPost();
// on without catch clause
} on LackOfPrivilegesException {
print("You don't have enough privileges.");
// on with catch clause
} on ValidationException catch (e) {
print('Please verify entered fields: ${e.fields}');
// general catch clause
} catch (e) {
print('Error occured: $e');
}
}
void sendPost() {
throw LackOfPrivilegesException();
}
class LackOfPrivilegesException implements Exception {}
class ValidationException implements Exception {
final List<String> fields;
const ValidationException(this.fields);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment