Debug School

rakesh kumar
rakesh kumar

Posted on

Different way to handle How do you handle errors in Dart

Handling errors in Dart often involves using exceptions, but there are different techniques to handle errors depending on the nature of the error and your application's requirements. Here are several ways to handle errors in Dart with examples:

  1. Using Exceptions:

Errors in Dart are often represented using exceptions. You can throw and catch exceptions to handle errors in your code.

try {
  // Code that may throw an exception.
  var result = 10 ~/ 0; // This will throw a `DivideByZeroException`.
} catch (e) {
  // Handle the exception.
  print('An exception occurred: $e');
}
Enter fullscreen mode Exit fullscreen mode

In this example, we catch the DivideByZeroException that occurs when dividing by zero.

  1. Custom Error Handling:

You can define custom error classes to handle specific types of errors in your application.

class MyCustomError implements Error {
  final String message;

  MyCustomError(this.message);

  @override
  String toString() => 'MyCustomError: $message';
}

void main() {
  try {
    // Code that may produce a custom error.
    throw MyCustomError('This is a custom error.');
  } catch (e) {
    // Handle the custom error.
    print(e); // Prints 'MyCustomError: This is a custom error.'
  }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we define a custom error class MyCustomError and throw and catch instances of it.

  1. Using on and catch Together:

You can use the on clause to catch specific types of errors and the catch clause to handle other errors.

try {
  // Code that may throw an exception.
  var result = int.parse('abc'); // This will throw a `FormatException`.
} on FormatException {
  // Handle a specific error type.
  print('Invalid format.');
} catch (e) {
  // Handle other errors.
  print('An error occurred: $e');
}
Enter fullscreen mode Exit fullscreen mode

In this example, we catch a FormatException specifically and use the general catch block for other errors.

  1. Error Callbacks:

Some asynchronous operations provide error callback functions to handle errors.

Future<void> fetchData() async {
  try {
    // Asynchronous code that may produce an error.
    var result = await fetchFromServer();
  } catch (e) {
    // Handle the error.
    print('An error occurred: $e');
  }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the fetchFromServer function returns a Future, and we use a try-catch block to handle any errors that occur during the asynchronous operation.

  1. Using assert for Debugging:

The assert statement can be used during development to check conditions and report errors if they occur.

void divide(int a, int b) {
  assert(b != 0, 'Cannot divide by zero.');
  print('Result: ${a ~/ b}');
}

void main() {
  divide(10, 2); // Result: 5
  divide(10, 0); // AssertionError: Cannot divide by zero.
}
Enter fullscreen mode Exit fullscreen mode

In this example, the assert statement checks that b is not zero and throws an AssertionError if the condition is not met.

Top comments (0)