Dart中的catchError捕获顺序

先贴一下该方法的源码:

  /**
   * Handles errors emitted by this [Future].
   *
   * This is the asynchronous equivalent of a "catch" block.
   *
   * Returns a new [Future] that will be completed with either the result of
   * this future or the result of calling the `onError` callback.
   *
   * If this future completes with a value,
   * the returned future completes with the same value.
   *
   * If this future completes with an error,
   * then [test] is first called with the error value.
   *
   * If `test` returns false, the exception is not handled by this `catchError`,
   * and the returned future completes with the same error and stack trace
   * as this future.
   *
   * If `test` returns `true`,
   * [onError] is called with the error and possibly stack trace,
   * and the returned future is completed with the result of this call
   * in exactly the same way as for [then]'s `onError`.
   *
   * If `test` is omitted, it defaults to a function that always returns true.
   * The `test` function should not throw, but if it does, it is handled as
   * if the `onError` function had thrown.
   *
   * Note that futures don't delay reporting of errors until listeners are
   * added. If the first `catchError` (or `then`) call happens after this future
   * has completed with an error then the error is reported as unhandled error.
   * See the description on [Future].
   */
  // The `Function` below stands for one of two types:
  // - (dynamic) -> FutureOr<T>
  // - (dynamic, StackTrace) -> FutureOr<T>
  // Given that there is a `test` function that is usually used to do an
  // `isCheck` we should also expect functions that take a specific argument.
  // Note: making `catchError` return a `Future<T>` in non-strong mode could be
  // a breaking change.
  Future<T> catchError(Function onError, {bool test(Object error)});
复制代码

翻译一下主要的意思,就是这个catchError方法可以捕获其他Futrue的异常信息,如果重写了test方法,test返回true就可以在catchError的onError方法里捕获到异常,如果test返回false,就把该异常继续抛出而不会在catchError方法里被捕获,如果不写test默认实现一个返回true的test方法,注意catchError只能捕获Future的异常,而不能捕获同步代码的异常,测试代码如下:

import 'dart:async';

Future testFutureError() {
  return new Future(() {
    throw "error";//1
//    return "abc";//2
  });
}

main() {
  testFutureError().then((value) {
    print("then " + value);
  }).catchError((e) {
    print("catchError " + e);
  }, test: (Object o) {
    print("test " + o);
    return true;//3
//    return false;//4
  }).catchError((e) {
    print("catchError2 " + e);
  }, test: (_) => true);
}
复制代码

如果是上面的代码,会输出

test error
catchError error
复制代码

因为//3这行返回了true,所以会在第一个catchError里被捕获。 如果//4不注释了,把//3注释,那么第一个catchError不能捕获该异常,该异常会继续抛出,然后在第二个catchError里被捕获。具体的可自行测试。

还有就是用whenComplete方法的时候,不是所有then都执行完再执行whenComplete方法,而是then、catchError、whenComplete这些方法会按照顺序(除非中途有异常会进入下一个catchError)执行,这一点和rxjava+retrofit不一样,原生开发请知悉。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值