close
0

How to create a custom exception and handle it in dart

avatar
asked 13-09-2021

2 Answers


0

following code works as expected (custom exception has been obtained is displayed in console) :

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

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

throwException() {
  throw new CustomException('This is my first custom exception');
}
avatar
answered 14-09-2021

0

Exception class if you don't care about the type of Exception. Simply fire an exception like this:

throw ("This is my first general exception");
avatar
answered 14-09-2021