当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Dart Zone.errorZone用法及代码示例


dart:async 库中Zone.errorZone 属性的用法介绍如下。

用法:

Zone errorZone

错误区负责处理未捕获的错误。

这是提供handleUncaughtError 方法的该区域最近的父区域。

异步错误永远不会跨越具有不同错误处理程序的区域之间的区域边界。

例子:

import 'dart:async';

main() {
  var future;
  runZoned(() {
    // The asynchronous error is caught by the custom zone which prints
    // 'asynchronous error'.
    future = Future.error("asynchronous error");
  }, onError: (e) { print(e); });  // Creates a zone with an error handler.
  // The following `catchError` handler is never invoked, because the
  // custom zone created by the call to `runZoned` provides an
  // error handler.
  future.catchError((e) { throw "is never reached"; });
}

请注意,错误也不能进入具有不同错误处理程序的子区域:

import 'dart:async';

main() {
  runZoned(() {
    // The following asynchronous error is *not* caught by the `catchError`
    // in the nested zone, since errors are not to cross zone boundaries
    // with different error handlers.
    // Instead the error is handled by the current error handler,
    // printing "Caught by outer zone: asynchronous error".
    var future = Future.error("asynchronous error");
    runZoned(() {
      future.catchError((e) { throw "is never reached"; });
    }, onError: (e) { throw "is never reached"; });
  }, onError: (e) { print("Caught by outer zone: $e"); });
}

相关用法


注:本文由纯净天空筛选整理自dart.dev大神的英文原创作品 errorZone property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。