Example of Try Catch to Handle Exception in Dart Flutter

Hello guys, We all know about Exceptions in a programming language. Exceptions are coding errors, Which terminates our program while it is running. To handle these type of situations we will be using Try Catch code block. It is as simple as we have seen in Cricket and Base ball, Where a player hits a ball using Bat and a fielder catches the ball to avoid making run by the bats man. This happens same in programming language when we usages Try Catch block. So in this tutorial we would learn about Example of Try Catch to Handle Exception in Dart Flutter.

Example of Try Catch to Handle Exception in Dart Flutter

Contents in this project Example of Try Catch to Handle Exception in Dart Flutter :-

There are basically 3 types of Try Catch Syntax available in Dart for exception handling.

1. Try Catch to handle Any Exception :

2. Try Catch to Handle Specific type of Exception :

3. Try Catch to Handle Multiple type of Exceptions Together :

Now after reading above examples. You know how easy is to Handle Exception in dart. But we would not stop here. I have explained you the syntax of Try and Catch block. But without an example it is not useful for beginners So let’s make an example of in dart How to use Try Catch for handling exceptions 🙂 .

1. Try Catch Example with Simple Exception Handling :

Output :

Output Without Try Catch :

2. Try Catch Example to handle Specific Exception :

Output :

Output Without Exception Handling :

So now friends, You all know how it is working. If you want to test this code right not then you can use Dart’s own online Dart Pad HERE. It will allow you to run dart code directly online.

Leave a Reply

Your email address will not be published.