It’s good to catch each kind of exception individually rather than catching all exceptions generally. Catching them individually allows you to handle them appropriately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // import 'dart:convert' as convert; // import 'package:http/http.dart' as http; try { final response = await http.get(url); if (response.statusCode != 200) throw HttpException('${response.statusCode}'); final jsonMap = convert.jsonDecode(response.body); } on SocketException { print('No Internet connection ?'); } on HttpException { print("Couldn't find the post ?"); } on FormatException { print("Bad response format ?"); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.