即将推出的名为 Zones 的功能在这里应该有所帮助。另外,请查看getAttachedStackTrace
.
此示例打印“catchError 内部”:
import 'dart:async';
void main() {
runZonedExperimental(() {
new Future.value(1)
.then((v) => v)
.then((v) => throw new ArgumentError('testing'))
.then((v) => v)
.catchError((e) => print('inside of catchError'));
},
onError: print);
}
此示例打印 'in onError':
import 'dart:async';
void main() {
runZonedExperimental(() {
new Future.value(1)
.then((v) => v)
.then((v) => throw new ArgumentError('testing'))
.then((v) => v);
},
onError: (e) => print('in onError'));
}
此示例打印“in onError: Illegal argument(s): testing”:
import 'dart:async';
void main() {
runZonedExperimental(() {
new Future.value(1)
.then((v) => v)
.then((v) => throw new ArgumentError('testing'))
.then((v) => v);
},
onError: (e) => print('in onError: $e'));
}
此示例打印出堆栈跟踪,其中包含原始异常发生的文件和行号:
#0 main.<anonymous closure>.<anonymous closure> (file:///Users/sethladd/dart/zoneexperiment/bin/zoneexperiment.dart:7:20)
编码:
import 'dart:async';
void main() {
runZonedExperimental(() {
new Future.value(1)
.then((v) => v)
.then((v) => throw new ArgumentError('testing'))
.then((v) => v);
},
onError: (e) => print(getAttachedStackTrace(e)));
}
区域应该在 1.0 之前退出实验。
getAttachedStackTrace 的文档:http: //api.dartlang.org/docs/releases/latest/dart_async.html#getAttachedStackTrace
runZoned 的文档:http: //api.dartlang.org/docs/releases/latest/dart_async.html#runZonedExperimental