In this lecture, I want to briefly touch on error, an exception kind of thing you can use to try except
health and find these statements for this.
So if you type trial followed by a column and then an indented block of code below, Python will try
to execute that code.
But if you just type tried colon and then the indented block of code below, you'll still get an error.
If the block of code fails, however, if you have an accept block or multiple accept blocks, you can
execute another block of code.
If an exception occurs, you can also specify Intel's block to run.
If everything goes well with the tribe, look and you can use a finally statement that will execute
a block of code, even if an exception occurs on the block.
OK, so let's demonstrate so I can print the result of one hundred divided by 10.
And that causes no problem.
I can't print the results of one or divided by zero zero division error.
So let me add a TradeBlock to this.
I can type train colon and then invent that.
So I still get an error pythons trying to execute the block, but it won't work.
I need to combine this with an acceptable look.
So let me add one.
So I can then type except colon and then print, something went wrong.
So now, instead of raising in error, I'm printing something went wrong.
So if you recall, we got a zero division error exception when we tried to print one hundred divided
by zero.
So let me show you
specifically zero division error.
We can actually add that specific exception to the except block by typing it after the except keyboard.
It's case sensitive.
So I can type except space, followed by the type of error.
Then I can be more specific, I can say.
You cannot divide by zero.
Now, as you can see.
We get a message saying you cannot divide by zero.
But no, this will now only catch exceptions that are zero two visionaries.
Any other exceptions?
Because both blocks to fail and we'll still get an error.
We cannot add block that will print if everything goes well in the tribe, look, you should come after
the except blocks so I can type else print.
Everything went well.
So let me change this so that I won't get in error.
So now, as you can see, it works.
I fixed the zero division error and the block also runs.
So not only do I print the result of one hundred divided by three, I also print the cells block.
I can also how to find liveblog after the blog, and this will print regardless of an exception.
So I can finally print.
This will always point.
So now let me raise an exception again.
So even though we go an exception, the finally block still printed a note the block didn't print this
time.
OK, so now let me create another kind of exception.
I'll try to divide by a string.
So this time we are a tight pair.
I can add another exception.
For type error to.
And this should come before the block.
So I can just type.
Great.
So now.
We print incompatible tapes because it raised a type error and we still print the final block.
This will always print.
OK, so that was a lecture on handling errors and exceptions in your blocks of code.
The good thing about the trial, except books, is that they prevent any errors if a block of code has
an exception.