Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page
  • Exception Handling with then (catchError)
  • Exception Handling with async await( try catch block)

Was this helpful?

  1. Dart Fundamentals -3
  2. Asynchronous Programming

Exception Handling in asynchronous code

Previousasync , await , thenNextFunctional Programming

Last updated 4 years ago

Was this helpful?

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



If you want to read more about error handling , here is the official documentation

Exception Handling with async await( try catch block)

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



For advanced error Handling , you should write a separate class which handles the error

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

Futures and error handlingdart_lang
Asynchronous programming: futures, async, awaitdart_lang
Logo
Logo