How to create a custom exception and handle it in dart

后端 未结 3 1523
死守一世寂寞
死守一世寂寞 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:35

    Dart code can throw and catch exceptions. In contrast to Java, all of Dart’s exceptions are unchecked exceptions. Methods don’t declare which exceptions they might throw, and you aren’t required to catch any exceptions.

    Dart provides Exception and Error types, but you’re allowed to throw any non-null object:

    throw Exception('Something bad happened.');
    throw 'Waaaaaaah!';
    

    Use the try, on, and catch keywords when handling exceptions:

    try {
      breedMoreLlamas();
    } on OutOfLlamasException {
      // A specific exception
      buyMoreLlamas();
    } on Exception catch (e) {
      // Anything else that is an exception
      print('Unknown exception: $e');
    } catch (e) {
      // No specified type, handles all
      print('Something really unknown: $e');
    }
    

    The try keyword works as it does in most other languages. Use the on keyword to filter for specific exceptions by type, and the catch keyword to get a reference to the exception object.

    If you can’t completely handle the exception, use the rethrow keyword to propagate the exception:

    try {
      breedMoreLlamas();
    } catch (e) {
      print('I was just trying to breed llamas!.');
      rethrow;
    }
    

    To execute code whether or not an exception is thrown, use finally:

    try {
      breedMoreLlamas();
    } catch (e) {
      // ... handle exception ...
    } finally {
      // Always clean up, even if an exception is thrown.
      cleanLlamaStalls();
    }
    

    Code example

    Implement tryFunction() below. It should execute an untrustworthy method and then do the following:

    • If untrustworthy() throws an ExceptionWithMessage, call logger.logException with the exception type and message (try using on and catch).
    • If untrustworthy() throws an Exception, call logger.logException with the exception type (try using on for this one).
    • If untrustworthy() throws any other object, don’t catch the exception.
    • After everything’s caught and handled, call logger.doneLogging (try using finally).

    .

    typedef VoidFunction = void Function();
    
    class ExceptionWithMessage {
      final String message;
      const ExceptionWithMessage(this.message);
    }
    
    abstract class Logger {
      void logException(Type t, [String msg]);
      void doneLogging();
    }
    
    void tryFunction(VoidFunction untrustworthy, Logger logger) {
      try {
        untrustworthy();
      } on ExceptionWithMessage catch (e) {
        logger.logException(e.runtimeType, e.message);
      } on Exception {
        logger.logException(Exception);
      } finally {
        logger.doneLogging();
    
    0 讨论(0)
  • 2021-02-11 14:41

    You don't need an 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");
    
    0 讨论(0)
  • 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');
    }
    
    0 讨论(0)
提交回复
热议问题