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.
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.
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:
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 exceptiona ~/ b;}// code for handling specific exceptionon IntegerDivisionByZeroException {print('Cannot divide a number by zero');}}
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 exceptiona ~/ b;}catch(e) {print(e);// Here we can write code to handle exceptions that occurs unexpectedly.}}
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 exceptionthrow new FormatException();}// code for handling specific exceptionon IntegerDivisionByZeroException {print('Cannot divide a number by zero');}catch(e) {print(e);// Here we can write code to handle exceptions that occurs unexpectedly.}}
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 exceptiona ~/ b;}// code for handling specific exceptionon IntegerDivisionByZeroException {print('Cannot divide a number by zero');}finally {print('Finally block executed!');}}
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.
What is the purpose of exception handling in Dart?
Prevent program crashes
Provide meaningful error messages
Facilitate debugging and troubleshooting
All of the above