Answers for "how to create a custom exception in dart"

2

dart custom exception

class CustomException implements Exception {
  String errorMessage() {
    return ("Invalid Amount");
  }
}

void AmountException(int amount) {
  if (amount <= 0) {
    throw new CustomException();
  }
}

void main() {
  try {
    AmountException(0);
  } catch (e) {
    print(errorMessage());
  }
}
Posted by: Guest on June-15-2021
1

dart custom exception

class CustomException implements Exception {
  String cause;
  CustomException(this.cause);
}

void main() {
  try {
    throwException();
  } on CustomException {
    print("custom exception is been obtained");
  }
}

throwException() {
  throw new CustomException('This is my first custom exception');
}
Posted by: Guest on July-27-2020

Code answers related to "how to create a custom exception in dart"

Browse Popular Code Answers by Language