Created
February 27, 2022 19:18
-
-
Save hotdang-ca/bdde285799650cc681e87a36cee14c3a to your computer and use it in GitHub Desktop.
Dart Async Throw and Rethrow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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