Skip to content

Instantly share code, notes, and snippets.

@kasperpeulen
Created August 7, 2015 02:02
Show Gist options
  • Select an option

  • Save kasperpeulen/0a4db293b07257514e3f to your computer and use it in GitHub Desktop.

Select an option

Save kasperpeulen/0a4db293b07257514e3f to your computer and use it in GitHub Desktop.
streams/throw_error
// Copyright (c) 2015, the Dart project authors.
// Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed
// by a BSD-style license that can be found in the LICENSE file.
import 'dart:async';
Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
try {
await for (var value in stream) {
sum += value;
}
} catch (error) {
return -1;
}
return sum;
}
Stream<int> countStream(int to) async* {
for (int i = 1; i <= to; i++) {
if (i == 4) {
throw "Whoops!"; // Intentional error
} else {
yield i;
}
}
}
main() async {
var stream = countStream(10);
var sum = await sumStream(stream);
print(sum); // -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment