Dart Error Handling

Dart Error Handling

ยท

1 min read

Error handling in Dart involves handling exceptions and errors when the program encounters an unexpected situation.

This is crucial to make the program robust and user-friendly.

Dart uses 'try-catch' blocks to catch exceptions, with the 'on' keyword for specific types and 'catch' for all types.

Here , I am attaching one example of try and catch block with on keyword.

void main()
{
  String userInput = "3,14";
  try{
      double num = double.parse(userInput);
      print("Squre value is $userInput * $userInput");
  }on FormatException catch (e){
    print("You have inavalid input , Enter valid value and try agian");
    print(e);
  }
  catch(e)
  {
    print("Somthing wrong happend in your output");
    print(e);
  }
}

In the above example you can see that we have use try , catch and on method to handle the error when it throw runtime.

ย