Dart Basic- Exception

Home /

Table of Contents

What is the exception?


An exception in Dart is an event that occurs during the execution of a program and disrupts the normal flow of instructions. Exceptions are used to handle errors or unexpected situations that arise during program execution.

In Dart, every exception is a subtype of the predefined class Exception. This means that all exceptions in Dart can be caught using a try-catch block. When an exception occurs, it must be handled to prevent the application from terminating abruptly.

Dart provides a try-catch block that allows developers to handle exceptions gracefully. The try block contains the code that may throw an exception. The catch block handles the exception if it occurs, and the finally block includes code that should be executed irrespective of an exception’s occurrence.

Handling exceptions properly is essential to ensure that the program runs smoothly and that users are provided with useful feedback in the event of an error.

An exception (or exceptional event) is a problem that arises during the execution of a program. 

Exception Handling in Dart


Exceptions are a powerful mechanism for handling runtime errors in a program. In Dart, every exception is a subtype of the pre-defined class Exception.

try-catch is used to execute a block of code that could throw an exception and handle it without letting the program terminate abruptly. The try block must be followed by exactly one on/catch block or one finally block, or one of both.

The syntax for handling an exception is as follows −


try {
// code that might throw an exception
}
on Exception1 {
// code for handling exception
}
catch Exception2 {
// code for handling exception
}


In this syntax, the try block contains the code that may throw an exception, and the on or catch block contains the code for handling the exception.

The on block is used to handle a specific exception. It is a block that is executed when a particular exception occurs. If the exception is not of the specified type, then it will not be caught.

The catch block is used to catch any exception that is thrown. This block executes only if an exception occurs and is of a type that is not handled by the on block.

Here’s an example of using the on block:

void main() {
  int x = 16;
  int y = 0;
  int res;
  try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException {
    print('Cannot divide by zero');
  }
}


In this example, we are trying to divide x by y, which is zero. Since division by zero is not allowed, it throws an IntegerDivisionByZeroException. We handle this exception using the on block and print the message “Cannot divide by zero”.

The finally block includes code that should be executed irrespective of an exception’s occurrence. The optional finally block executes unconditionally after try/on/catch.

Here’s an example of using the finally block:


try {
// code that might throw an exception
}
on Exception1 {
// exception handling code
}
catch Exception2 {
// exception handling
}
finally {
// code that should always execute; irrespective of the exception
}


In this example, the finally block contains code that should always execute, irrespective of whether an exception is thrown or not. It is useful for releasing resources like files, network sockets, etc., which must be closed even if an exception occurs.

Exception handling is an important feature of Dart that helps to ensure that our programs can handle runtime errors gracefully without crashing.

Properties of Exceptions

NoExceptions & Description
1DeferredLoadException
Thrown when a deferred library fails to load.
2FormatException
Exception thrown when a string or some other data does not have an expected format and cannot be parsed or processed.
3IntegerDivisionByZeroException
Thrown when a number is divided by zero.
4IOException
Base class for all Inupt-Output related exceptions.
5IsolateSpawnException
Thrown when an isolate cannot be created.
6Timeout
Thrown when a scheduled timeout happens while waiting for an async result.
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article