errorZone 属性
错误区域负责处理未捕获的错误。
这是离此区域最近的父区域,该区域提供了 handleUncaughtError 方法。
在 futures 中的异步错误不会在不同错误处理器的区域之间跨越区域边界。
示例
import 'dart:async';
main() {
var future;
runZonedGuarded(() {
// The asynchronous error is caught by the custom zone which prints
// 'asynchronous error'.
future = Future.error("asynchronous error");
}, (error) { print(error); }); // Creates a zone with an error handler.
// The following `catchError` handler is never invoked, because the
// custom zone created by the call to `runZonedGuarded` provides an
// error handler.
future.catchError((error) { throw "is never reached"; });
}
请注意,错误也无法进入具有不同错误处理器的子区域
import 'dart:async';
main() {
runZonedGuarded(() {
// 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");
runZonedGuarded(() {
future.catchError((e) { throw "is never reached"; });
}, (error, stack) { throw "is never reached"; });
}, (error, stack) { print("Caught by outer zone: $error"); });
}
实现
Zone get errorZone;