All right.
So in the last video we looked at try catch and finally and I used the example of if else if and else.
Now you might wonder why even use try and catch if I can just really make sure that all of these problems
never occur.
So none of the exceptions ever happen.
Well, the thing is, there is a level to which you can do that, and then there is a level where you
cannot do it anymore.
So you can obviously check.
Does the connection work.
You can check.
Do we have the right input from the user?
Is it in the right format?
Is it in the right like is the number within the right range and all of that stuff?
You can go very, very deep into it.
But at one point there might be a situation where there are just exceptions that you cannot manage anymore.
So you have control over certain things in the user input, for example, or in how well your connection
is or whatever.
But at one point you don't have the full control over it anymore, so you cannot control how the user
is using their phone.
So, or their PC or whatever, or you don't have control over the internet connection of a user.
You don't have control over whether a user by accident plugs out the USB drive of which you were trying
to read data.
Okay, so in all of these cases, try and catch block is the better solution.
However, when you can catch the errors, so to speak, or when you can avoid the errors to even happen,
then use if else blocks or you use the control flow, so to speak, to control the flow of your application
of how the user is entering data, of how the whole app is running, how the software is running that
you're building.
So the rule of thumb is use if and else if and else blocks whenever.
You can control the outcome or the data, the flow, so to speak.
But if you cannot control it, use try and catch.
Also something to consider.
A try and catch is significantly slower in terms of execution time than an if statement, so take that
as additional info when writing your code.