Google News
logo
Dart - Interview Questions
How do you handle exceptions in Dart?
In Dart, exceptions are handled using try-catch blocks. The try block contains the code that may potentially throw an exception, and the catch block catches and handles the thrown exception. Here's the basic syntax for exception handling in Dart:
try {
  // Code that may throw an exception
} catch (exception) {
  // Exception handling code
}​


Let's explore the different aspects of exception handling in Dart :

1. Throwing Exceptions :

   Exceptions can be thrown using the `throw` keyword followed by an instance of an exception or an object that implements the `Exception` class. Dart provides various built-in exception classes, such as `Exception`, `Error`, and their subclasses, or you can define custom exception classes by extending the `Exception` class or any of its subclasses.
   void divide(int a, int b) {
     if (b == 0) {
       throw Exception('Division by zero is not allowed.');
     }
     print(a / b);
   }​

   In the example above, the `divide` function throws an instance of the `Exception` class with a custom error message if the second argument `b` is zero.


2. Catching Exceptions :

   Exceptions are caught and handled using catch blocks. The catch block specifies the exception type that it can handle, and the caught exception can be accessed using a named identifier. Multiple catch blocks can be chained to handle different types of exceptions.
   try {
     // Code that may throw an exception
   } catch (exceptionType) {
     // Exception handling code for exceptionType
   } catch (anotherExceptionType) {
     // Exception handling code for anotherExceptionType
   }​


   In the catch block, you can provide specific exception types to catch specific exceptions or use the `on` keyword to catch exceptions of specific types.
   try {
     divide(10, 0);
   } catch (e) {
     print('Exception occurred: $e');
   }​

   In the example above, the `divide` function is called within the try block. If an exception is thrown, it will be caught in the catch block, and the error message will be printed.
3. Finally Block :

   Additionally, you can use a `finally` block to specify code that should always be executed, regardless of whether an exception is thrown or not. The code in the `finally` block executes regardless of whether an exception is caught or not.
   try {
     // Code that may throw an exception
   } catch (exception) {
     // Exception handling code
   } finally {
     // Code that always executes
   }​

   The code in the `finally` block is useful for releasing resources or performing cleanup operations.


4. Rethrowing Exceptions :

   Dart allows you to rethrow caught exceptions using the `rethrow` keyword. This allows you to catch an exception, perform additional operations or logging, and then rethrow the same exception to be caught at a higher level.
   try {
     divide(10, 0);
   } catch (e) {
     print('Exception occurred: $e');
     // Perform additional operations or logging
     rethrow; // Rethrow the caught exception
   }​

   In the example above, the caught exception is printed, and additional operations or logging can be performed before rethrowing the exception.
Advertisement