Why can’t I catch this Exception?

Catching exceptions in Flutter/Dart

Is there a difference when using await?

void main() {
try {
catchMeIfYouCan();
} catch (e) {
print(e.toString());
}
}
Future<void> catchMeIfYouCan() async {
await Future.delayed(Duration(seconds: 1), () {
throw Exception('did you?');
});
}
Uncaught Error: Exception: did you?
void main() async {
try {
await catchMeIfYouCan();
} catch (e) {
print(e.toString());
}
}
void main() {
catchMeIfYouCan().onError((e, _) {
print(e.toString());
});
}
yield* someMethod(someParameter);
import 'dart:async';void main() {
runZonedGuarded(() {
catchMeIfYouCan();
}, (e, s) {
print("Gotcha!!! :)");
});
}
Gotcha!!! :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store