Exception Handling in asynchronous code
Exception Handling with then (catchError)
void main() {
//Exception Handling
print(" Start fetching images");
getData().then((images) {
return images + " " + "caption";
}).then((captionedimages) {
print(captionedimages + " " + " save to disk");
}).catchError((err){
// Logic for handling error
print("Error : $err , Sorry for the error , check if you are connected to internet");
});
print("Loading Images");
}
Future<String> getData() {
Future<String> a = Future<String>.delayed(Duration(seconds: 5), () {
// Logic
print(" Image Fetched !!!!!");
throw "404 Error";
});
return a;
}
Exception Handling with async await( try catch block)
Last updated