Welcome in this lecture, I'm going to build a program that will handle errors properly, OK, stop.
They say that the program will receive an age value as an argument from the command line like this,
a choice that marks the first argument.
OK.
Here, ages type is drink, so I need to convert it to an integer before I can use it, right.
So let's convert it using a TOI function like this and hour short, declare Starcom a two I h.
As you can see, I see the returned values from this function into two variables and are most of the
time and error value is saved into a variable names error is in here.
Is it not from the previous lecture that if the user passes a non numeric number eight to able to return
an error?
Right.
So now it is time to check for that within a simple if statement like this.
And as I said before, the air value would be in until one day to our function couldn't convert the
past value.
So here I'm checking whether there was an error or not by comparing the error variables value with a
new value.
Remember, nominal value means there is an error.
So this branch will only be executed when the user passes a non numeric value.
Now, let's display the returned air value like this printer error.
I'm going to pass to everybody, OK?
Let's quit from the main function just by returning from it like this, OK, value call a function that
returns an error.
You should always check for the error immediately after the call to that function, as I did so here.
OK, so you shouldn't do anything else before checking for the error.
It's because you may create unintentional bugs in your software if you don't check for the errors immediately.
All right, now let's print another message about that, the commercial my successful like this print
F.
To success.
Converted.
Person to person to nearline.
Now, I'm going to pass H on here.
OK, so if the program reaches to this code line, that means the error value was near and so to add
to our function was successful.
OK.
This is also known as the happy path.
This is what we want from this program, right?
So to convert the number without any problems, so that makes us happy.
So it's called a happy.
But let's right now.
Let's run it with a proper numeric value like this, just 25.
As you can see, Prince success, cool, as I said, this was the program Bapat, so there wasn't any
error.
The conversion was successful and the error, it was new.
Now, let's run this program.
It's an incorrect data like this example, it prints an error message is because now the error value
wasn't nil.
So there was an error.
It was executed.
This if statement.
And then it has returned from the program here, so the happy part wasn't executed because there was
another.
So in summary, this is how we can handle errors Engo by using a simple if statement to check for whether
that returned error value is nil or not, and that's all about it.
All right, thank you for watching.
See in the next picture.