An exception is a runtime error or warning condition, which can be predefined or user-defined. Predefined exceptions are raised implicitly (automatically) by the runtime system. User-defined exceptions must be raised explicitly by RAISE statements. To handle raised exceptions, you write separate routines called exception handlers.

Your Dart code can throw and catch exceptions. Exceptions are errors indicating that something unexpected happened. If the exception isn’t caught, the isolate that raised the exception is suspended, and typically the isolate and its program are terminated.

In contrast to Dart, all of Dart’s exceptions are unchecked exceptions. Methods don’t declare which exceptions they might throw, and you aren’t required to catch any exceptions.

Dart provides Exception and Error types, as well as numerous predefined subtypes. You can, of course, define your own exceptions. However, Dart programs can throw any non-null object—not just Exception and Error objects—as an exception.

An Exception is intended to convey information to the user about a failure, so that the error can be addressed programmatically. It is intended to be caught, and it should contain useful data fields.   Creating instances of Exception directly with Exception(“message”) is discouraged in library code since it doesn’t give users a precise type they can catch. It may be reasonable to use instances of this class in tests or during development.

There are many type of pre-defined exceptions present in dart, they are – IOException,IsolateSpawnException,NullRejection,ExceptionOSErrorTimeoutException etc.

Let’s see some of their usages below…

Code 1:

void main(){
  int a=10/0;
  print(a);
}
output
From the upper code, see integer 10 can’t divisible by 0. Along Dart by-default returns double as output on the basics of division.

Let’s see another exception (FileSystemException)

Code 2:
import ‘dart:async’;
import ‘dart:io’;
 
void main() {
  File(‘file.txt’).readAsString().then((String contents) {
    print(contents);
  });
}
output

Let’s difference between exceptions and errors