How to handle exceptions in Dart

Exception handling plays a vital role in reliable code execution. When an exception occurs, the regular flow of the program is disrupted, and the program stops abnormally. By handling exceptions effectively, we can gracefully recover from errors, prevent program crashes, provide meaningful error messages to users, and facilitate debugging and troubleshooting.

Try / on / catch

The try-block contains code that could throw an exception. The on-block is utilized when the specific exception type needs to be handled. On the other hand, the catch-block is used when the exception object needs to be handled.

There are a few things that need to be kept in mind while handling exceptions.

  • The try-block must be followed with either one on/catch-block or one finally block (or both).

  • Multiple exceptions can be handled in a code snippet by using multiple on/catch-blocks.

Syntax

try { 
   // This block contains the code that can throw an exception 
}  
on Exception1 { 
   // code for handling specific exception 
}  
catch GenericException { 
   // code for handling exceptions that can unexpectedly occur.
} 

Now we will explain exception handling with code illustration using different blocks one by one:

Using on-block

We will write a code where a number will be divided by zero. It will cause an exception in try block name IntegerDivisionByZeroException
as we can also create an on block to handle this specific exception.

main() {
int a = 5;
int b = 0;
try {
// code that can throw an exception
a ~/ b;
}
// code for handling specific exception
on IntegerDivisionByZeroException {
print('Cannot divide a number by zero');
}
}

Using catch-block

We will write a code where a number will be divided by zero, which will cause an exception in try block. Since we don't know the name of the error, we will catch the error and print it on the output in the catch block.

main() {
int a = 5;
int b = 0;
try {
// code that can throw an exception
a ~/ b;
}
catch(e) {
print(e);
// Here we can write code to handle exceptions that occurs unexpectedly.
}
}

Using on-catch-block

We will write a code to throw an exception FormatException using the throw keyword to cause an exception intentionally so that the on block should not be executed because it specifically deals with IntegerDivisionByZeroException exception. A generic catch block will run and print the exception name in the output.

main() {
int a = 5;
int b = 1;
try {
// code that can throw an exception
throw new FormatException();
}
// code for handling specific exception
on IntegerDivisionByZeroException {
print('Cannot divide a number by zero');
}
catch(e) {
print(e);
// Here we can write code to handle exceptions that occurs unexpectedly.
}
}

Using finally block

The finally block contains the code that will be executed regardless of whether an exception occurs. This block is optional and always executes after the try/on/catch blocks.

main() {
int a = 5;
int b = 0;
try {
// code that can throw an exception
a ~/ b;
}
// code for handling specific exception
on IntegerDivisionByZeroException {
print('Cannot divide a number by zero');
}
finally {
print('Finally block executed!');
}
}

Conclusion

In Dart, exception handling is essential for ensuring reliable code execution. We can recover from errors, prevent crashes, provide meaningful error messages, and facilitate debugging by effectively handling exceptions. Using the try/on/catch blocks, we can handle exceptions based on their specific types or generic exceptions. Additionally, the finally block allows us to execute code regardless of an exception's occurrence.

1

What is the purpose of exception handling in Dart?

A)

Prevent program crashes

B)

Provide meaningful error messages

C)

Facilitate debugging and troubleshooting

D)

All of the above

Question 1 of 30 attempted
Copyright ©2024 Educative, Inc. All rights reserved