Welcome back. Now, remember that there are
three main kinds of errors that we can get when writing Python code.
There are syntactic errors.
That's when Python doesn't understand how to read the instructions in your program.
So, for example, if I forget a bracket or a parenthesis and I tried to run my program,
then I get a syntax error saying that
Python didn't understand the instructions that I gave it.
Beyond syntactic error, there are also runtime errors.
Runtime errors occur when Python understood the instructions that I gave it,
but something went wrong when Python tried to run those instructions.
So, for example, if I run my code right now,
I get a runtime error or more specifically
an index error because even though Python understands that I'm
creating a list and assigning that list to items and then
I'm assigning third to be the third item in items,
because item only has two items, a and b,
when I tried to fetch index two,
then I get an index error which is the kind of runtime error.
So, in other words, Python understood what I wanted to do,
but something went wrong as Python was trying to execute my code.
Then finally, there are semantic errors.
In a semantic error is when Python successfully ran the code,
it just wasn't exactly what I wanted it to do.
So, for example, if I wrote a function to add
two numbers and that function actually divided two numbers,
Python isn't going to give me an error but I'm also not going to
be happy when my code divides the numbers that I wanted to add.
So, when we have a syntactic error,
Python doesn't run our code at all.
When we have a runtime error,
Python runs our code until it encounters the runtime error.
Then we have a semantic error,
Python runs our code but it just doesn't end up being what we wanted it to be.
Now, usually when we encounter a runtime error,
then Python is going to stop running our code as soon as we encounter the runtime error.
So, for example, this code,
again, gives us an index error.
So, let's suppose that I modified my code and I had something like print,
this is not going to be reached.
Now, if I save and run my code,
you'll notice that this line three doesn't print
out because this expression results in a runtime error.
So, again, when you get a runtime error right here,
then our program just stops running and throws in this case an index error.
If I had a print statement before this line,
this is going to be printed,
then this would successfully execute.
So, in this lesson we're going to learn about Try Except.
What Try Except does,
is it allows us to handle runtime errors and
to tell Python what to do when it encounters a runtime error,
so it doesn't suddenly stop running your program every time it encounters one.
So, let's say that I wanted to run the code after assigning third.
So, let's suppose that again I have a print,
I want this to run statement and I
want this line to print out even if I encounter a runtime error on line two.
The way that I can do that is by putting that into a Try Except block.
So, the simplest way to write a Try Except block is just by saying,
try and then colon and then code inside of the try block,
and then I can write except,
colon, and then code that I want to run if some runtime error happens.
So, let's suppose that if I get a runtime error while running line four,
let's suppose that I want to assign third to be false.
So, I'll say except third equals false.
Now, when I run my code,
it's not going to show us a runtime error anymore,
but instead I actually enabled a reach
this print statement even though I get a runtime error right here.
So, to see what's happening,
I'm going to add a few more print statements.
So, inside of this except I'm going to print something went wrong,
and I'm going to add print a on line
four and after assigning third,
I'm going to print out b.
Okay. When I run my code,
you'll notice that I have print a, print a executes.
Then when I set third equal to items two,
this gives us a runtime error.
As a result of this runtime error,
print b you'll see, never executes.
So, what happens is,
as soon as I get a runtime error for any line in this try-block,
then Python jumps right to
the except block and starts running what's in the except block.
So, you'll see that something went wrong,
gets executed and then here we assign third to be false,
and then I want this to run,
gets executed after that.
Now, let's suppose that we didn't actually have a runtime error.
So, I'm going to clear
my output and I'm going to make it so that items actually does have three items.
Now, if I run my code,
then what happens is,
I never get a runtime error when assigning
third because I actually have a third item here.
So, a gets printed,
third gets assigned to c,
and then b gets printed,
and then we completely skipped the except block because nothing went wrong,
and then we print out,
I want this to run.
So, in brief, what Try Except does,
is it says try to run the code inside of this try block and if
something goes wrong while you're running that code
rather than stopping execution of the whole program,
just jump and run whatever is inside of the except block.
If nothing goes wrong,
then we just skip the except block entirely and run what's after the Try Except.
That's all for now, until next time.