Hi again! Let's have one more lecture about errors in Python and this
particular lecture will be about handling errors. Handling errors can be
an advanced concept to digest for now because you're still going through the
basics of Python and you haven't seen any real application yet, but later
will use error handling while we are building real-world applications so
you're going to understand error handling better later on. For now let's
just focus on the syntax and try to understand as much as you can
I'll go and do an example here which I mentioned earlier. Let's say we have a
function that does division between two given numbers like that. Return a divided
by b and then you call the function here. Divide 1, 2. In this case the
function will try to divide 1 by 2. And let me print out the output so that
we can see it on the terminal. Go to the terminal and execute the script. So you
get you get 0.5. Now that's good. However if the user enters let's say
zero there like that, execute and your program will crash so it will show an
error. Now this one here is an example of a longer error message that I mentioned
previously in the previous videos that you might have more than one, more
than one error in your traceback, so for instance this says line 4 at this line
here. So that line is where Python captured the error at first
so basically Python was trying to execute this line but it couldn't do it,
it didn't manage to do it because this line was trying to get the information
from this function and this function was trying to do this
mathematical operation using zero after the division so that is not possible and
Python is pointing that out here. So that's the main error in line two
in this expression so that's where you should focus, however when you ask
questions you should include the entire error message in your question
as I mentioned previously. So back to handling errors now. What I'm
trying to say here is that you know you have other functions here and other
lines of code that you want to execute, that you want Python to execute, but if
the user passed this zero number here the other lines would not be executed
so your program would crash and you don't want that and the way to avoid
that is by using try and accept so basically you try to capture the
exception, to handle the exception. The way to do that is quite easy.
You say try and return a/b, so this has to be indented like that so try is indented
because it's under the function definition. And then return is indented
because it's under the try expression. Then you have except, execute so this
has to be on the same indentation level with try, they are the same block, so
except if there is an exception. What you want to do is print out: "You are
dividing by zero", or "is this meaningless". So let me try
this now and you get: "Zero division is meaningless" and none, oh sorry I meant
to do here return, not print. I explained you why you get none there so you
got none as an output because what the function did was its tried to do
this expression but it got an error so it ignores this line, it doesn't
execute it and then it goes to the next line. So this line is executed when
this is not executed and so it printed out: "Zero division is meaningless", but it
also printed out none because when you execute a function that doesn't have a
return statement such as in this case we had the print statement here, the print
function not the return statement, in that case if the function is not returning
anything it can have a print function or other function as well, but if it
doesn't have a return statement then it will print out none, so basically
it executes the other procedures such as the print statement, it prints out the
statement but if it doesn't have a return statement it doesn't, it prints
out none. Anyway save this now, execute again and in this case you get only this
zero division is meaningless printed out there. So basically if you had other
lines of code here like that, those would be executed as well and that happens by
using the try and except block who which make sure that the code flow continues.
So while the important thing to note is that it is advisable to actually here use
the exact name of the error that you want to accept, so
Zero Division Error and as you see these got turned into blue which means that
this is actually a Python keyword, is recognized by Python and in that case if
you exact you this you get the same output actually like that or if you pass
a number there that it is possible to get a division from, you'll get that.
Sorry about the backslash. I typed that by mistake on my terminal. Execute
again so you get this. And so what I'm trying to say about the zero division
error is that you know sometimes you might get other types of errors in your
code, other types of exceptions. Let's say you have a name error there
and you are saying that except all errors when you don't pass this you
you're basically including all types of errors there and that is a bit dangerous for your
code because you know you might have problems that you don't notice and
basically you are ignoring that, but you shouldn't. So you should be specific
there, explicit like that. If there are other types of errors such as a name error,
that name error will crash your program, so you know what's going on.
You need to be specific. If you want to handle that error too, then
you can add another exception here. Except name error for instance. Yeah,
that's about handling errors in Python. I hope this makes sense! I hope I explained
this well! And yeah, this is about errors. I'll talk to you in the next lectures.
See you!