So now that we are done with Variables, Types and Functions in Dart (if you haven’t read it, please read it here), now we will take a look at control flow and exception handling in Dart.

I highly recommend going through the official language tour on Dart website after reading this tutorial series. You can find the Official Language tour here.

Control Flow in Dart

If – else

The if-else in dart is pretty straight forward and very similar to other languages.

You also have the short form of writing an if-else conditions using the ternary operator.


Dart supports all kinds of loops and you will be familiar with the syntax since it is same as many other languages.

For loop

The classic for loop.

While loop

The classic for while loop.

Do-while loop

The classic for do while loop.


Never miss a post from TheTechnoCafe

Exception Handling

Dart use the typical try-catch block to handle exceptions and uses the throw keyword to cause an exception.

Throwing Exception

First lets us see how can we throw an exception in Dart.

When the value of integer b is 0 then we throw an inbuilt Exception named IntegerDivisionByZeroException.

You can also just throw the Exception object itself with a message string.

Catching and Handling Exceptions

Now comes the main part of catching and handling exceptions.

A particular type of exception can be caught by using the on keyword as follows.

If you don’t know the type of Exception that will be thrown, or are uncertain then use catch block to handle any kind of exception.


Dart also provides a finally block that will always be executed no matter if any exception is thrown or not.

That is it for this tutorial. Read the next tutorial.

Next Tutorial >> Just enough Dart for Flutter – Tutorial 03 – Classes and Generics.

Previous Tutorial << Just enough Dart for Flutter – Tutorial 01 – Variables, Types and Functions.

Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!


Dustin · June 18, 2018 at 4:51 pm

> So now that we are don’t

“don’t” = “done”

Just enough Dart for Flutter - Tutorial 01 - Variables, Types and Functions - TheTechnoCafe · June 12, 2018 at 7:00 pm

[…] Next Tutorial >> Just enough Dart for Flutter – Tutorial 02 – Control Flow and Ex… […]

Leave a Reply

Your email address will not be published. Required fields are marked *

five × two =