【发布时间】:2020-03-31 09:46:21
【问题描述】:
try {
final result = await http.get(pingUrl)
.catchError((e) => print(e));
return result;
} catch (e) {
print(e)
}
为什么我不能在 catch 块中处理异常?
【问题讨论】:
标签: asynchronous flutter dart async-await
try {
final result = await http.get(pingUrl)
.catchError((e) => print(e));
return result;
} catch (e) {
print(e)
}
为什么我不能在 catch 块中处理异常?
【问题讨论】:
标签: asynchronous flutter dart async-await
不,因为你正在追赶未来。
你以后不应该撒谎:
try {
final result = await http.get(pingUrl);
// .catchError((e) => print(e)); <- removed
return result;
} catch (e) {
print(e)
}
或者在catchError中再次抛出:
try {
final result = await http.get(pingUrl)
.catchError((e) {
print(e);
throw foo;
});
return result;
} catch (e) {
print(e)
}
我更喜欢第一个选项。
【讨论】: