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.
1 2 3 4 5 6 7 8 9 10 11 |
main(List<String> args) { var number = 57; if (number > 100) { print('Large Number'); } else if (number < 100) { print('Small Number'); } else { print('Number is 100'); } } |
You also have the short form of writing an if-else conditions using the ternary operator.
1 2 3 4 5 |
main(List<String> args) { int age = 60; String status = age < 50 ? "Still young" : "Old Man"; } |
Looping
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.
1 2 3 4 5 |
main(List<String> args) { for (int i = 0; i < 10; i++) { print('$i'); } } |
While loop
The classic for while loop.
1 2 3 4 5 6 7 |
main(List<String> args) { int i = 0; while(i < 10) { print('$i'); i++; } } |
Do-while loop
The classic for do while loop.
1 2 3 4 5 6 7 |
main(List<String> args) { int i = 0; do { print('$i'); i++; } while (i < 10); } |
Switch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
main(List<String> args) { int age = 50; switch(age) { case 10: print('Too Young.'); break; case 20: case 30: print('Still Young!'); break; case 40: print('Getting old.'); break; case 50: print('You are old!'); break; } } |
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.
1 2 3 4 5 6 7 8 9 10 |
main(List<String> args) { divide(10, 0); } divide(int a, int b) { if (b == 0) { throw new IntegerDivisionByZeroException(); } return a / b; } |
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.
1 2 3 4 5 6 7 8 9 10 |
main(List<String> args) { divide(10, 0); } divide(int a, int b) { if (b == 0) { throw new Exception('Divide by zero'); } return a / b; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
main(List<String> args) { try { divide(10, 0); } on IntegerDivisionByZeroException { print('Division by zero.'); } } divide(int a, int b) { if (b == 0) { throw new IntegerDivisionByZeroException(); } return a / b; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
main(List<String> args) { try { divide(10, 0); } on IntegerDivisionByZeroException { print('Division by zero.'); } catch (e) { print(e); } } divide(int a, int b) { if (b == 0) { throw new Exception('Some other exception.'); } return a / b; } |
Finally
Dart also provides a finally block that will always be executed no matter if any exception is thrown or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
main(List<String> args) { try { divide(10, 0); } on IntegerDivisionByZeroException { print('Division by zero.'); } catch (e) { print(e); } finally { print('I will always be executed!'); } } divide(int a, int b) { if (b == 0) { throw new Exception('Some other exception.'); } return a / b; } |
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.
2 Comments
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… […]