> For the complete documentation index, see [llms.txt](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-3/untitled/exception-handling-in-asynchronous-code.md).

# Exception Handling in asynchronous code

## Exception Handling with then (catchError)

{% embed url="<https://youtu.be/CfwBXW6B0SQ>" %}

```dart

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;
}




```

If you want to read more about error handling , here is the official documentation&#x20;

{% embed url="<https://dart.dev/guides/libraries/futures-error-handling>" %}

## Exception Handling with async await( try catch block)

{% embed url="<https://youtu.be/mQ84q03BFP4>" %}

```dart
void main() async {
  
  // Hadling exception
  
  
  try{
  var images = await fetchImage();
  print(images);
  var capImages = await captionImages(images);
  print(capImages);
  var saved = await savetodisk(capImages);
  print(saved);  
    
  }
  
  catch(err){
    
    // logic for handling error 
    
    print("Error $err , check if you are connected to internet");
    
    
  }
  

}




Future<String> fetchImage() async {
  await Future<String>.delayed(const Duration(seconds: 5));
  throw "500 error";
}


Future<String> captionImages(images) async {
  await Future<String>.delayed(const Duration(seconds: 5));
  return images+" "+"caption";
}


Future<String> savetodisk(capImages) async {
  await Future<String>.delayed(const Duration(seconds: 5));
  return capImages+" "+"Saved";
}




```

{% hint style="info" %}
For advanced error Handling , you  should write a separate class which handles the error
{% endhint %}

You should go through the following codelab to get a better understanding of asynchronous programming in dart :-&#x20;

{% embed url="<https://dart.dev/codelabs/async-await#example-completing-with-an-error>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-3/untitled/exception-handling-in-asynchronous-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
