Dart Custom Exception Handling Class with Example

3
5774
Dart Custom Exception Handling
Exception Handling with a custom Class

Dart offers built-in methods to handle the exceptions but Dart custom exception handling class can give you more control.

Exceptions can be different based on your logic and project. Let say you are building a school grading system. Marks cannot be negative values. It is just one example. Another example could be total marks. Obtained marks cannot be more than total marks. In both cases, it is an exception to our grading system.

We have covered exception handling in detail with examples. It is advised to go through once more. It is an extension of that article.

Dart Custom Exception Handling Class

Now we have created a custom Class named GradeException and it implements the Exception interface. The GradeException class has only one method that returns a string message that marks cannot be negative.

  
 class GradeException implements Exception {
  String errorMessage() {
    return 'Marks cannot be -ve values';
  }

Let’s write the main function with a simple checkMarks function which throws an exception of type GradeException. Run the code.

  
 void main() {
  checkMarks(-20);
}

void checkMarks(int marks) {
  if (marks < 0) throw GradeException();
}

class GradeException implements Exception {
  String errorMessage() {
    return 'Marks cannot be -ve values';
  }
}

Here is the result. As the marks are -ve values and exception is not handled. An expected Unhandled exception is thrown.

  
 Unhandled exception:
Instance of 'GradeException'
#0      checkMarks (file:///Users/Waheed/IdeaProjects/FlutterRDart/bin/main.dart:10:18)
#1      main (file:///Users/Waheed/IdeaProjects/FlutterRDart/bin/main.dart:3:3)
#2      _startIsolate. (dart:isolate/runtime/libisolate_patch.dart:289:19)
#3      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

It is time to handle this exception and return a message of our own choice. Again simply use a try-catch block to handle the exception or try-on block. Here is the code with try-catch block.

  
 void main() {
  try {
    checkMarks(-20);
  } catch (e) {
    print(e.errorMessage());
  }
}

void checkMarks(int marks) {
  if (marks < 0) throw GradeException();
}

class GradeException implements Exception {
  String errorMessage() {
    return 'Marks cannot be -ve values';
  }
}

Run the code and BOOM check the console. The exception is handled in a pretty much better way.

  
Marks cannot be -ve values

Write a MarksMoreThanTotal function with proper exception handling and share your experience.

We hope you have enjoyed learning this cool Dart Custom Exception Handling Class tutorial. Please do let us know.

3 COMMENTS

  1. in recent version 2.13.0 when code is run it shows error

    ” test.dart:5:13: Error: The method ‘errorMessage’ isn’t defined for the class ‘Object’.
    – ‘Object’ is from ‘dart:core’.
    Try correcting the name to the name of an existing method, or defining a method named ‘errorMessage’.
    print(e.errorMessage());
    ^^^^^^^^^^^^ “

  2. I get the following error:

    line 5 • The method ‘errorMessage’ isn’t defined for the type ‘Object’. (view docs)
    Try correcting the name to the name of an existing method, or defining a method named ‘errorMessage’.

LEAVE A REPLY

Please enter your comment!
Please enter your name here