How to use try-catch in dart:
try-catch is used to handle exceptions in programming languages. In dart also, we have try-catch and we can use it to handle different types of exceptions.
It is always a good idea to use try-catch blocks for exception handling. If you are creating a flutter app and if it raise an exception for some reason, the app will crash. Which is not good experience for the users. So, if we add try-catch for all parts of the program where it might throw exceptions, we can deliver a crash-free application.
In this post, I will give you an overview of try-catch with examples. I will also show you how to use on and finally keywords with try-catch.
Example of try-catch:
try-catch statements consists of two blocks of code. The first block, which is for try holds the main code that we are executing. It will try to run that code. If everything works fine, i.e. if no exception is raised, it will move out of the try-catch block and execute other part of the program.
The catch block will execute only if any exception is thrown in the try block. Inside this block, we can have access to the exception object and print the cause of the exception.
Let’s take a look at the below program:
void main() {
var given_list = ['3', '4', '5', 'hello'];
try {
for (int i = 0; i < given_list.length; i++) {
print(int.parse(given_list[i]));
}
} catch (e) {
print("Exception raised !! $e");
}
}
Here,
- given_list is a list of strings.
- We are using a for loop in the try block and trying to parse the string values as integer and printing the result.
- The catch block is handling if any exception is raised. It is also printing the exception result e.
If you run this program, it will print the below output:
3
4
5
Exception raised !! FormatException: hello
The loop worked for 3, 4, and 5. But for ‘hello’, it throws FormatException and moved to the catch block. After the catch block, the program execution will continue.
We can use the catch block to log errors, to send the exception to our crash analytics server or to show a message to the user.
on Block:
on block can handle any known exception. For example, we know the above exception FormatException. We can use a on block to handle this exception.
If the exceptioin thrown in the try block matches any exception of on blocks, it will move to that block for execution. Else, it will move to catch block.
void main() {
var given_list = ['3', '4', '5', 'hello'];
try {
for (int i = 0; i < given_list.length; i++) {
print(int.parse(given_list[i]));
}
} on FormatException{
print("FormatException raised !! Please check the values of the list. ");
}
catch (e) {
print("Unknown Exception raised !! $e");
}
}
It will print:
3
4
5
FormatException raised !! Please check the values of the list.
It moved to the on block since we got one FormatException.
finally:
finally block executes always. This block is written at the end. i.e. after try-on-catch.
void main() {
var given_list = ['3', '4', '5', 'hello'];
try {
for (int i = 0; i < given_list.length; i++) {
print(int.parse(given_list[i]));
}
} on FormatException {
print("FormatException raised !! Please check the values of the list. ");
} catch (e) {
print("Unknown Exception raised !! $e");
} finally {
print("Executing finally block.");
}
}
It will print:
3
4
5
FormatException raised !! Please check the values of the list.
Executing finally block.
Here,
- First the on block is executed once the exception is raised.
- Then finally is executed.
Conclusion:
I hope that you learned how to use try-on-catch-finally block in dart. Handling exceptions is important in any programming language and it is a good development practice.