metadeck
10/4/2018 - 10:36 AM

Beginners Dart - Try Catch Finally

Beginners Dart - Try Catch Finally

void main(){

  //Error is a program failure.
  //Exemption - can be handeled.

  //Without defining age, we will be presented with a no such method excemption.

  try {

    int age; 
    int dogYears = 7;

    print(age * dogYears);

  } on NoSuchMethodError {

    //Here we have a valueless definition, which will compile as an exemption error.
    print("Sorry that's not going to happen.");

  } catch(e) {

    //Here we report that there is an error and try to resolve it.
    print("There was an error: ${e.toString()}");

  } finally { 

    //Without a resolution the execption then outputs the word complete.
    print("complete.");

  }

}