當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。