飞镖未来和重新抛出
Dart future and rethrow
是否有任何 rethrow
(不是 throw
)等价于 dart 中的期货?
myFunc()
.then(processValue)
.catchError(handleError);
是的。
如果一个catchError
throws the same error object again (because it acts the same way as then
's onError
)的handleError
,它被认为是“重新抛出”并且它也会保留原始堆栈跟踪。
如果你只想捕获一些类型的错误,并重新抛出其余的,你可以使用catchError
:
的测试参数
myFunc()
.then(processValue)
.catchError(handleError, test: (e) => e is MyException);
这只会捕获 MyException
异常并让任何其他错误通过,您无需重新抛出它们。 test
可以进行任何测试,而不仅仅是类型检查。
此外,如果您使用 async
函数,您可以只使用 rethrow
:
...) async {
try {
var processValue = await myFunc();
....
} catch (e) {
if (something(e)) {
whatnot();
} else {
rethrow;
}
}
是否有任何 rethrow
(不是 throw
)等价于 dart 中的期货?
myFunc()
.then(processValue)
.catchError(handleError);
是的。
如果一个catchError
throws the same error object again (because it acts the same way as then
's onError
)的handleError
,它被认为是“重新抛出”并且它也会保留原始堆栈跟踪。
如果你只想捕获一些类型的错误,并重新抛出其余的,你可以使用catchError
:
myFunc()
.then(processValue)
.catchError(handleError, test: (e) => e is MyException);
这只会捕获 MyException
异常并让任何其他错误通过,您无需重新抛出它们。 test
可以进行任何测试,而不仅仅是类型检查。
此外,如果您使用 async
函数,您可以只使用 rethrow
:
...) async {
try {
var processValue = await myFunc();
....
} catch (e) {
if (something(e)) {
whatnot();
} else {
rethrow;
}
}