Error Handling

Error Handling in Dart

Introduction

Error handling is crucial for writing robust Dart applications. Proper error handling helps manage unexpected conditions gracefully, ensuring your application can recover from or respond to errors effectively.

About

In Dart, errors are represented by the Error and Exception classes.

  • Errors: Generally represent serious issues that your application should not try to handle (e.g., OutOfMemoryError).
  • Exceptions: Represent conditions that can be handled or recovered from (e.g., FileSystemException).

Dart uses try-catch blocks to handle exceptions and finally blocks to execute code regardless of whether an exception occurred.

How to Use

Key Components

  • try: Block where code that might throw an exception is executed.
  • catch: Block where you handle the exception.
  • on: Handles specific exceptions if needed.
  • finally: Block that executes code regardless of whether an exception occurred.

Syntax

try {
  // Code that may throw an exception
} catch (e) {
  // Code to handle the exception
} on SpecificException catch (e) {
  // Handle specific exceptions
} finally {
  // Code that runs regardless of success or failure
}

How It Works

  • Try Block: Encloses code that might throw an exception.
  • Catch Block: Handles exceptions thrown by the try block. You can catch all exceptions or specific types.
  • On Block: Provides a way to handle specific exceptions differently.
  • Finally Block: Executes code that should run regardless of whether an exception was thrown or not (e.g., resource cleanup).

Example

  • Example 1: Basic Exception Handling
void main() {
  try {
    int result = 10 ~/ 0; // This will throw an exception
  } catch (e) {
    print('An error occurred: $e');
  }
}

Example 2: Handling Specific Exceptions

void main() {
  try {
    String fileContent = readFile('non_existent_file.txt');
  } on FileSystemException catch (e) {
    print('File system error: $e');
  } catch (e) {
    print('An unexpected error occurred: $e');
  }
}

String readFile(String path) {
  // This is a dummy implementation
  throw FileSystemException('File not found');
}

Example 3: Using Finally

void main() {
  try {
    // Code that might throw an exception
    print('Trying to execute code.');
  } catch (e) {
    print('An error occurred: $e');
  } finally {
    // This block will always execute
    print('Finally block executed.');
  }
}

Explanation:

  1. Basic Exception Handling:
  • The catch block handles any exception thrown by the code in the try block.
  1. Handling Specific Exceptions:
  • The on block allows you to handle specific types of exceptions, such as FileSystemException, separately from general exceptions.
  1. Using Finally:
  • The finally block ensures that certain code (e.g., cleanup code) is executed whether or not an exception occurred.

Summary

  • Use try-catch blocks to manage exceptions and ensure your application can handle errors gracefully.
  • The on block allows for handling specific exceptions, while the finally block ensures certain code always runs.
  • Proper error handling improves application reliability and user experience by gracefully managing unexpected conditions.

This format provides a clear and concise overview of error handling in Dart, including practical examples and key concepts.