gpt4 book ai didi

http - 关于 HttpException 没有被捕获

转载 作者:行者123 更新时间:2023-12-03 04:45:31 26 4
gpt4 key购买 nike

我正在尝试处理 http 错误,所以我创建了自定义 http 异常类

class HttpException implements Exception {
final String message;
HttpException(this.message);
@override
String toString() {
return message;
}
}

并将其抛出 http 错误
   Future<void> createProfile(Profile profile) async {
try {
var request =
new http.MultipartRequest("POST", Uri.parse(APIPath.createProfile()));
...
final response = await request.send();
if (response.statusCode != 201) {
...
throw HttpException(jsonResponse["error"]);
}

notifyListeners();
} catch (error) {
print(error.runtimeType); //<= prints HttpException
throw error;
}
}

当我试图捕捉它时,它只被捕捉到异常而不是 HttpExeption
      try {
await Provider.of<User>(context, listen: false).createProfile(profile);

} on HttpException catch (error) {
print('Http exception'); //<- this is never reached
} on Exception catch (error) {
print(error.runtimeType); // <= prints HttpException
print('exception'); //<- http exception caught here;
} catch (error) {
print('error');
}

有没有机会在 HttpException 上处理 http 异常?

最佳答案

HttpException来自 dart-io 的类(class)被引用而不是自定义的“HttpException”。

关于http - 关于 HttpException 没有被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62409337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com