Skip to content

Instantly share code, notes, and snippets.

@martynov-alex
Last active October 9, 2024 07:44
Show Gist options
  • Select an option

  • Save martynov-alex/19c38e1cc5ace323310e91bec2972f25 to your computer and use it in GitHub Desktop.

Select an option

Save martynov-alex/19c38e1cc5ace323310e91bec2972f25 to your computer and use it in GitHub Desktop.
await-unawait-ignore
import 'dart:async';
Future<void> main() async {
await runZonedGuarded(
() async {
try {
await call();
} catch (e) {
print('$e поймана в catch внутри runZonedGuarded');
}
f1();
await f1();
f2();
await f2();
f3();
await f3();
},
(e, _) {
print('$e поймана в runZonedGuarded');
},
);
}
Future<void> call() async {
try {
f1();
await f1();
f2();
await f2();
f3();
await f3();
} catch (e) {
print('$e поймана в catch функции call');
}
}
// ignore()
// Ошибка будет проигнонирована, кроме внутреннего try-catch
Future<void> f1() async {
print('f1');
Future.delayed(Duration(seconds: 1), () {
try {
throw Exception('e1-1');
} catch (e) {
print('$e поймана в catch функции f1');
}
throw Exception('e1-2'); // будет везде проигнорирована
}).ignore();
}
// Без await (unawaited)
// Ошибка будет поймана только в runZonedGuarded
Future<void> f2() async {
print('f2');
unawaited(Future.delayed(Duration(seconds: 1), () => throw Exception('e2')));
}
// C await
// Ошибка будет поймана в первом try-catch, если его нет, то в runZonedGuarded
Future<void> f3() async {
print('f3');
await Future.delayed(Duration(seconds: 1), () => throw Exception('e3'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment