Skip to main content

Command Palette

Search for a command to run...

Exception Handling in Dart: A Beginner's Guide

Updated
4 min read

Errors and exceptions are inevitable in any application. Exception handling ensures that your program can handle unexpected situations gracefully without crashing. In this guide, we will explore how exception handling works in Dart, common exceptions, and best practices for managing them.


What is an Exception?

An exception is an event that disrupts the normal flow of a program. In Dart, exceptions are objects that represent errors or unexpected conditions during program execution. Handling these exceptions allows your application to recover or provide meaningful feedback to the user.


Why Use Exception Handling?

Exception handling helps to:

  • Prevent your program from crashing unexpectedly.

  • Provide meaningful error messages.

  • Allow the program to recover from errors.

  • Improve user experience.


Try-Catch Blocks in Dart

Dart uses try, catch, and finally blocks to handle exceptions.

Syntax

try {
  // code that might throw an exception
} catch (e) {
  // handle the exception
} finally {
  // optional cleanup code
}

Example

void main() {
  try {
    int result = 10 ~/ 0; // Division by zero
    print(result);
  } catch (e) {
    print('An exception occurred: $e');
  } finally {
    print('Execution completed.');
  }
}

Output:

An exception occurred: IntegerDivisionByZeroException
Execution completed.

Handling Specific Exceptions

You can handle specific exceptions by specifying the type in the catch block.

Example

void main() {
  try {
    List<int> numbers = [1, 2, 3];
    print(numbers[5]); // Accessing an invalid index
  } on RangeError {
    print('Index is out of range!');
  } catch (e) {
    print('An exception occurred: $e');
  }
}

Output:

Index is out of range!

In this example, a RangeError is handled separately.


Using the finally Block

The finally block contains code that will execute whether or not an exception occurs. It is useful for cleanup tasks.

Example

void main() {
  try {
    int result = 10 ~/ 2;
    print(result);
  } catch (e) {
    print('An exception occurred: $e');
  } finally {
    print('Cleanup completed.');
  }
}

Output:

5
Cleanup completed.

Throwing Exceptions

In Dart, you can throw exceptions manually using the throw keyword.

Example

void checkAge(int age) {
  if (age < 18) {
    throw Exception('You must be at least 18 years old.');
  }
}

void main() {
  try {
    checkAge(16);
  } catch (e) {
    print('Error: $e');
  }
}

Output:

Error: Exception: You must be at least 18 years old.

In this example, an exception is thrown if the age is less than 18.


Custom Exceptions

You can create your own custom exception classes to represent specific errors.

Example

class InvalidInputException implements Exception {
  final String message;
  InvalidInputException(this.message);

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

void validateInput(String input) {
  if (input.isEmpty) {
    throw InvalidInputException('Input cannot be empty.');
  }
}

void main() {
  try {
    validateInput('');
  } catch (e) {
    print(e);
  }
}

Output:

InvalidInputException: Input cannot be empty.

In this example, a custom exception InvalidInputException is created and used for validation.


Common Dart Exceptions

Here are some common exceptions in Dart:

  • FormatException: Occurs when a string is improperly formatted.

  • NoSuchMethodError: Thrown when a method or property does not exist.

  • RangeError: Occurs when accessing an invalid index in a list.

  • StateError: Thrown when the state of an object is invalid for a method call.

  • UnsupportedError: Thrown when an unsupported operation is attempted.

Example

void main() {
  try {
    int number = int.parse('abc'); // Invalid format
    print(number);
  } catch (e) {
    print('Exception: $e');
  }
}

Output:

Exception: FormatException: Invalid radix-10 number (at character 1)
abc
^

Best Practices for Exception Handling

  1. Use Specific Exceptions: Handle specific exceptions whenever possible.

  2. Avoid Overusing Exceptions: Only use exceptions for truly exceptional cases.

  3. Provide Meaningful Error Messages: Ensure error messages are clear and helpful.

  4. Log Exceptions: Log exceptions for debugging and monitoring purposes.

  5. Use finally for Cleanup: Always use finally for cleanup tasks like closing files or releasing resources.


Conclusion

Exception handling is crucial for building robust and user-friendly applications. By understanding how to handle exceptions effectively in Dart, you can ensure that your applications are reliable and maintainable. Start experimenting with try-catch blocks and create custom exceptions to handle errors specific to your application.

Happy coding!

More from this blog

S

Suneel D.K

25 posts

Learn Flutter and Dart with easy tutorials and YouTube videos! Master app development with tips, tricks, and detailed guides for beginners and pros alike. Start building today!