我正在尝试处理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异常?
发布于 2020-06-16 23:33:31
引用的是来自dart-io的HttpException类,而不是自定义的“HttpException”。
https://stackoverflow.com/questions/62409337
复制相似问题