1

I want to handle the exception from the 'onData' function(the first positional parameter), but the onError is not fired when the exception is thrown. I just got an 'Unhandled Exception' warning.

Am I misunderstanding the 'onError' on usage? When will it be fired?

The codes:

final Logger log = Logger('test logger');
client.getUrl(Uri.parse(url)).then((HttpClientRequest request) {
      return request.close();
    }).then((HttpClientResponse response) {
      try {
        response.listen((d) {
          throw Exception("TEST EXCEPTION");
        }, onDone: () {
          log.finer("onDone"); 
        }, onError: (e, stack) {
          log.warning("onError"); 
        }, cancelOnError: true);
      } catch (e, stack) {
        log.warning("listen"); 
      }
    }).catchError((e, stack) {
      log.warning("future"); 
    });
RockingDice
  • 1,909
  • 2
  • 14
  • 22
  • `Stream#listen` docs say: `"On errors from this stream, the onError handler is called with the error object and possibly a stack trace."` - so it catches errors which are for example produced by [addError](https://api.dartlang.org/stable/2.4.1/dart-async/StreamController/addError.html) method or similar, see [StreamSink](https://api.dartlang.org/stable/2.4.1/dart-async/StreamSink-class.html) for more info – pskink Aug 10 '19 at 03:10
  • yes, it makes sense, thank you. Looks like I treat exceptions as errors, but they are different concepts. – RockingDice Aug 10 '19 at 06:02

0 Answers0