Skip to content

Instantly share code, notes, and snippets.

@kevmoo
Last active December 3, 2020 19:54
Show Gist options
  • Save kevmoo/c8deb87cee33b48316fc0f5cf4b1891f to your computer and use it in GitHub Desktop.
Save kevmoo/c8deb87cee33b48316fc0f5cf4b1891f to your computer and use it in GitHub Desktop.
Future<void> main() async {
for (var func in [goodCatch, badCatch]) {
print('Running $func');
try {
await func();
} catch (e) {
print('Why was this not caught? $e');
}
print('');
}
}
Future<void> goodCatch() async {
try {
// `await` here is CRITICAL!
return await badAsync();
} on StateError catch (e) {
print('Caught "$e"!');
}
}
Future<void> badCatch() async {
try {
// No await, so the state error is not caught!
return badAsync();
} on StateError catch (e) {
print('Caught "$e"!');
}
}
Future<void> badAsync() async {
throw StateError('bad!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment