📜  dart try-catch - Dart (1)

📅  最后修改于: 2023-12-03 15:00:20.111000             🧑  作者: Mango

Dart Try-Catch

When writing programs, it's important to expect and handle errors, or exceptions, that may occur. In dart, we use the try-catch block to do this.

Try Block

The try block is where we write our code that may throw an exception. If an exception is thrown, the rest of the statements in the try block will not be executed. Here's an example:

try {
  var result = 10 ~/ 0; // Throws an integer division by zero exception
  print(result);
} catch (e) {
  print('Error: ${e.toString()}'); // Prints the error message
}

In this example, we try to divide 10 by 0 which will throw an integer division by zero exception. The catch block will handle this exception by printing the error message.

Catch Block

The catch block is where we handle the exception. It starts with the catch keyword followed by a parameter which represents the exception object. We can use this parameter to get information about the exception such as the message, stack trace, etc.

try {
  var result = 10 ~/ 0; // Throws an integer division by zero exception
  print(result);
} catch (e) {
  print('Error: ${e.toString()}'); // Prints the error message
}

In this example, the catch block catches the exception thrown in the try block and prints its message using e.toString().

Finally Block

The finally block is optional, and it runs whether or not an exception was thrown. It's useful for cleaning up resources that were used in the try block.

try {
  // Some code that may throw an exception
} catch (e) {
  // Handling the exception
} finally {
  // Code that will always be executed
}

In this example, the finally block will always be executed, whether or not an exception was thrown in the try block. It's useful for things like closing files, releasing memory, or disconnecting from a database.

Multiple Catch Blocks

It's possible to have multiple catch blocks for different types of exceptions.

try {
  // Some code that may throw an exception
} on Exception catch (e) {
  // Handling the Exception
} on FormatException catch (e) {
  // Handling the FormatException
} catch (e) {
  // Handling all other exceptions
}

In this example, there are three different catch blocks. The first will handle any exception that is an instance of the Exception class. The second will handle FormatExceptions specifically, and the final catch block will handle any exceptions not caught in the first two blocks.

Rethrowing Exceptions

We can also re-throw an exception using the rethrow keyword. This allows the exception to continue propagating up the call stack, where it can be caught by another catch block.

try {
  // Some code that may throw an exception
} catch (e) {
  // Re-throwing the exception
  rethrow;
}

In this example, any exception thrown in the try block will be re-thrown in the catch block, allowing it to be caught by a higher-level catch block.

Conclusion

The try-catch block is an essential tool for handling exceptions in dart programs. By using the try-catch block, we can ensure that our programs handle errors gracefully and continue running, even when something unexpected happens.