Welcome to another video of our pipeline lessons.
Today, we are going to talk about the try and accept statement, try and accept is very useful in case
we want to try a block of code.
But we also know that we might get an error for any reason, and that error will cause our program to
crash.
Let's create a wireless program that should be working unless we interrupt the program using control.
See?
This program takes input from the user using the raw input function, then we split it using the split
function and we get the first and second element.
Then finally we printed.
This program should keep running, but what happens if we provide only one string, then the second
string, this line won't work and it should give us an error because it will try to fix something that
simply doesn't exist.
Let's try that.
As you noticed, we got to hear the name of the errors called index error, followed by a short description
of the better.
That's all we need from now.
Now, let's tell our program to expect something like that and deal with it using the try and accept
Buck.
First, we execute the block of code that we know we might get an error from under the TRAI statement,
let's invent all the lines.
Then after the block, we must provide an exception.
We do that by typing the word accept,
but if we only provide the word except, then our program will deal with any type of error in general
and it will jump into this line, would it, when it occurs?
Now, let's be more specific and provide which type of where we want to deal with.
From before we know that there was we received this called indexer, so let's specify that after the
word, except
we can also get the description of the air that will help us in our troubleshooting later.
We can do that by using the word as followed by the variable name that we will store the air in.
Now our air will be stored in the variable E. We can also print it.
Nice.
We can also use the Elst statement in the trial, except block, the statement will be executed if nothing
went wrong with the tribe block, but we still want to execute.
We can also use the finally block, which it will be executed regardless of the try block raises in
here or not.
Now, let's try it both within here and without error.
As you noticed, the final block was executed in both times.
We have reached the end of this lesson.
Thank you.
And see you in the next one.