What is "catch" in Dart?

Thu, Dec 15, 2022

Read in 3 minutes

In this post we take a look at "catch" keyword with some example codes.

In Dart, catch is a keyword that is used to catch and handle exceptions that occur during program execution. Exceptions are events that occur when the program encounters an error or an unexpected condition that prevents it from executing normally.

The catch keyword is used in conjunction with a try block, which defines a block of code that may potentially throw an exception. If an exception is thrown within the try block, the program will jump to the catch block that matches the type of the thrown exception.

Here is the syntax for a try-catch block in Dart:

try {
  // code that may throw an exception
} catch (exceptionType) {
  // code to handle the exception
}

In this syntax, the try block contains the code that may throw an exception. If an exception is thrown within the try block, the program jumps to the catch block, which specifies the type of exception that it can handle. If the thrown exception matches the type specified in the catch block, the code in the catch block is executed.

Here’s an example of how try-catch works in Dart:

try {
  var result = 5 ~/ 0; // This will throw an exception
  print(result);
} catch (e) {
  print('An error occurred: $e');
}

In this example, the try block attempts to divide 5 by 0, which is not a valid mathematical operation and will therefore throw an exception. The catch block specifies a catch-all exception type (e), which will match any exception that is thrown within the try block. When the exception is thrown, the program jumps to the catch block and the string 'An error occurred: ...' is printed to the console.

You can also specify specific exception types in the catch block, like this:

try {
  var result = int.parse('hello');
  print(result);
} on FormatException catch (e) {
  print('Invalid number: $e');
} on Exception catch (e) {
  print('An error occurred: $e');
}

In this example, the try block attempts to parse the string 'hello' into an integer, which will throw a FormatException. The catch block specifies two different exception types: FormatException, which will match the thrown exception in this case, and Exception, which will match any other exception that is thrown within the try block. When the exception is thrown, the program jumps to the appropriate catch block based on the type of the thrown exception.

In summary, the catch keyword in Dart is used to catch and handle exceptions that occur during program execution. It is used in conjunction with a try block, which defines a block of code that may throw an exception. When an exception is thrown within the try block, the program jumps to the catch block that matches the type of the thrown exception, and executes the code in that block to handle the exception.


Shohruh AK



Category:

Dart

Tags:



See Also

What is "case" in Dart?
What is "break" in Dart?
How to make HTTP "POST" request in Flutter
What is "await" in Dart?
What is "async" in Dart?