Skip to content

Instantly share code, notes, and snippets.

@madmag77
Last active March 6, 2021 09:44
Show Gist options
  • Save madmag77/940e9fa7a39a816e67169a4f9ebf2887 to your computer and use it in GitHub Desktop.
Save madmag77/940e9fa7a39a816e67169a4f9ebf2887 to your computer and use it in GitHub Desktop.
[Dart article] Exceptions in streams 2
import 'dart:async';
void main(List<String> arguments) async {
Stream<int> stream;
runZonedGuarded(() {
stream = getStream();
}, (e, s) {
print('PRODUCER ZONE Exception handled: $e, $s');
});
runZonedGuarded(() {
startListener(stream);
}, (e, s) {
print('CONSUMER ZONE Exception handled: $e, $s');
});
}
Stream<int> getStream() {
final intsController = StreamController<int>.broadcast();
int i = 42;
Timer.periodic(Duration(seconds: 1), (timer) {
i += 1;
if (i == 46) throw '[Stream] Bad thing happens!';
intsController.add(i);
});
return intsController.stream;
}
void startListener(Stream<int> ints) {
ints.listen((event) {
print(event);
if (event == 44) {
throw '[Listener] Bad thing happens!';
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment