How to create a custom exception and handle it in dart

后端 未结 3 1518
死守一世寂寞
死守一世寂寞 2021-02-11 13:51

I have written this code to test how custom exceptions are working in the dart.

I\'m not getting the desired output could someone explain to me how to handle it??

3条回答
  •  隐瞒了意图╮
    2021-02-11 14:43

    You can look at the Exception part of A Tour of the Dart Language.

    The 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');
    }
    

提交回复
热议问题