In this chapter, we will learn about the dart exceptions and why it is important to handle those exceptions.
Dart Exceptions
An exception is an issue that may occur when we run the program. Exception is a run-time error.
When an exception occurs, the program terminates. An example of an exception is when we try to divide a number by zero.
Types of Dart Exceptions
Here are the built-in dart exceptions:
Exception | Description |
DefferedLoadException | Thrown when a deferred library fails to load |
FromatException | Exception thrown when a string or some other data does not have an expected format and cannot be parsed or processed |
IntegerDivisionByZeroException | Thrown when a number is divided by zero |
IOEException | Base class for all Inupt-Output related exceptions |
IsolateSpawnException | Thrown when an isolate cannot be created |
Timeout | Thrown when a scheduled timeout happens while waiting for an async result |
Handling Exceptions
It is very important to handle and manage exceptions. Dart provides various methods to handle those exceptions.
Some of them are:
The try / on / catch Blocks
The syntax for handling an exception is:
try {
// code that might throw an exception
}
on Exception1 {
// code for handling exception
}
catch Exception2 {
// code for handling exception
}
The block of code that might throw an exception is used inside the try block.
The on block helps in specifying the exceptions.
The catch block helps to handle the error that occurs in the try block.
Example: try with catch Block
void main() {
int a = 20;
int b = 0;
int result;
try {
result = a ~/ b;
print(result);
}
catch(e) {
print("Error");
print(e);
}
}
Output
Error
Unsupported operation: Result of truncating division is Infinity: 12 ~/ 0
In the above example, we have tried to divide the integer value 20 with 0. This results in an exception.
When the exception occurs, the program flow moves to the catch block.
The Finally Block
The syntax of finally block is:
try {
// code that may be throw an exception
}
on Exception1 {
// exception handling code or specifying the exception
}
catch Exception2 {
// code for exception handling
}
finally {
// code that should always execute; whether exception or not.
}
The finally block always executes after the try/on/catch whether there is an exception or not.
Example: Finally Block with Exception
void main() {
int a = 20;
int b = 0;
int result;
try {
result = a ~/ b;
print(result);
}
catch(e) {
print("Error");
print(e);
}
finally {
print("Finally is executed always.");
}
}
Output
Error
Unsupported operation: Result of truncating division is Infinity: 20 ~/ 0
Finally is executed always.
In the above example, an exception arises and that exception is handled by the catch block.
The code inside finally block is also executed.
Example: Finally block without Dart Exception
void main() {
int a = 20;
int b = 2;
int result;
try {
result = a ~/ b;
print(result);
}
catch(e) {
print("Error");
print(e);
}
finally {
print("Finally is executed always.");
}
}
Output
10
Finally is executed always.
In the above example, there is no any exceptions. Also, the finally block is executed.
The finally block is useful when we want to execute a block of code in all conditions, whether there is a run-time error or not.