Dart Try Catch

Dart try-catch is used to execute a block of code that could throw an exception, and handle the exception without letting the program terminate.

If an exception, thrown by any of the code, is not handled via catch block, then the program could terminate because of the exception.

In this tutorial, we will learn how to use try-catch in Dart code, to catch the exceptions thrown, if any, by a piece of code enclosed in the try block.

Syntax

The syntax of try-catch to catch any type of exception is

try {
  //code that has potential to throw an exception
} catch(e) {
  //code
}

The syntax of try-catch to handle specific exception is

try {
  //code that has potential to throw an exception
} on SomeException {
  //code
}

The syntax of try-catch to handle multiple exceptions is

try {
  //code that has potential to throw an exception
} on SomeException {
  //code
} on AnotherException {
  //code
} catch(e) {
  //code
}
ADVERTISEMENT

Examples

Simple Try-Catch

In the following example, we write a method to display a string passed to it. If the given string is empty, this method throws an Exception. We handle calls to this function using a try-catch block.

main.dart

void displayName(str) {
  if (str.length > 0) {
    print(str);
  } else {
    throw new Exception('Name is empty.');
  }
}

void main() {
  var name = '';
  try {
    displayName(name);
  } catch (e) {
    print('There is an exception.');
  }
  print('Bye');
}

Output

There is an exception.
Bye

If we do not use try-catch, then the program would have terminated, as shown in the following.

main.dart

void displayName(str) {
  if (str.length > 0) {
    print(str);
  } else {
    throw new Exception('Name is empty.');
  }
}

void main() {
  var name = '';
  displayName(name);
  print('Bye');
}

Output

Unhandled exception:
Exception: Name is empty.
#0      displayName (file:///Users/tutorialkart/Desktop/Projects/DartTutorial/main.dart:10:5)
#1      main (file:///Users/tutorialkart/Desktop/Projects/DartTutorial/main.dart:16:3)
#2      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Try-Catch that handles Specific Exception

In the following example, we write a method to display a string passed to it. If the given string is empty, this method throws a custom Exception, i.e., EmptyNameException. We handle calls to this function using a try-catch block.

main.dart

class EmptyNameException implements Exception {
  String cause;
  EmptyNameException(this.cause);
}

void displayName(str) {
  if (str.length > 0) {
    print(str);
  } else {
    throw new EmptyNameException('Name is empty.');
  }
}

void main() {
  var name = '';
  try {
    displayName(name);
  } on EmptyNameException {
    print('Given name is empty.');
  }
  print('Bye');
}

Output

Given name is empty.
Bye

Conclusion

In this Dart Tutorial, we have learnt how to handle exceptions using try-catch, with examples.