Dart Exception Handling | RRTutors

Dart Exception Handling

Similar to other programing languages dart also we can handle exceptions by

Try, catch blocks and throw exceptions by throw keyword

 

main(List<String> args) {

  divide(10, 0);

divide(int a, int b) {

  if (b == 0) {

    throw new IntegerDivisionByZeroException();

  }

  return a / b;

}

 

Let’s catch the exception pass catch block

 

main(List<String> args) {

  try {

    divide(10, 0);

  } on IntegerDivisionByZeroException {

    print('Division by zero.');

  }

}

divide(int a, int b) {

  if (b == 0) {

    throw new IntegerDivisionByZeroException();

  }

  return a / b;

}

Advertisements