Skip to content

Instantly share code, notes, and snippets.

@hotdang-ca
Created February 27, 2022 19:18
Show Gist options
  • Save hotdang-ca/bdde285799650cc681e87a36cee14c3a to your computer and use it in GitHub Desktop.
Save hotdang-ca/bdde285799650cc681e87a36cee14c3a to your computer and use it in GitHub Desktop.
Dart Async Throw and Rethrow
Future<void> doAThing() async {
await Future.delayed(Duration(milliseconds: 500));
try {
print('Trying a dangerous thing...');
await doADangerousThing();
} catch (e) {
print('There was a minor error: $e');
rethrow;
}
}
Future<void> doADangerousThing() async {
await Future.delayed(Duration(milliseconds: 500));
print('here comes trouble...');
try {
throw ArgumentError('I want to argue!');
} catch (e) {
print('There was an error: $e');
rethrow;
}
}
Future<void> main() async {
print('Let us begin...');
try {
print('Trying a thing...');
await doAThing();
} catch (e) {
print('Something bad happened: $e');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment