Welcome back, everyone to this lecture or we're going to be discussing errors and exception handling.
When people are using our website later on, we learn about jingo.
There are going to be instances where we don't have complete information to fulfill their web page request,
and we need to make sure the site functions can still operate even if an error were to occur.
When you're dealing with something like a website that has user interactions.
You can't know every single possible user interaction ahead of time.
So you have to assume that sometimes the user may make a mistake or we just don't have a web page for
what the user is looking for.
For example, you've seen Web pages that return a 404 error page if the page doesn't exist, or maybe
the user entered the wrong domain URL extension.
We don't want our entire website just to break down.
So we need a way of actually taking into account that an error may occur.
We'll need a way to try to execute a block of code, but continue to another block if the code doesn't
work.
Now, you've definitely already seen Python issue an exception error when you've made a small programming
mistake when learning Python.
And this is where Python tries to inform you of the specific error taking place.
For example, i o error, which is an input output error, or maybe even a type error, which is the
wrong data types are trying to work together.
And you can actually write code for specific error situations so you can actually try to preemptively
understand or relay a message for specific errors that you think may come out ahead of time.
Now, in order to do this with Python, we actually use three keywords.
It is the tri keyword and this is the block of code to be attempted, which may or may not lead to an
error.
There is the accept keyword, which is the block of code.
We're going to execute in case there is an error in the try block.
And then there's also an optional finally block of code, and that's a final block of code to be executed
regardless of an error.
Let's explore understanding exception codes and how to use try, except finally blocks to navigate potentially
unknown errors in the future.
I'm going to head over to Visual Studio now.
All right, here I am in Visual Studio.
Now there is a specific type of error that we can't really actually try to catch and then continue to
do something.
And that specifically is a syntax error.
So I want to quickly point that out right now that you can't really catch this error because this particular
type of error doesn't even allow the Python code to actually compile.
And that's the kind of error where you're saying something like print.
And then maybe for some reason you forgot to actually close off that particular block of code.
So you say something like, hello.
But note that I never actually have that closing quote.
And in fact, Visual Studio code is actually trying to warn me of this.
So it says, Hey, there's a problem here.
No quick fix unless you actually close off that quote.
But I'm going to save this, and I want to point out this sort of error where you just your code is
actually not even be able to compile.
That's something we can't actually catch.
We have to fix that in our code before we actually run anything.
But I do want to run this so you can see what an exception looks like.
So I'm going to type out Python example that pi hit this and know this.
This is specifically a syntax error.
And it says end of line while scanning string literal.
So you never actually figured out, Hey, where is the end of the quote?
Their syntax errors are not something we can particularly try to grab in advance because it doesn't
let the code run to begin with.
So what kind of errors can we actually run in advance?
Well, let's imagine I'm trying to print out the results of 10 plus 10, but maybe somewhere along the
line I accidentally and trying to do the string 10 plus the integer 10.
Now this may make sense if maybe we actually grabbed user input, but for whatever reason, we forgot
to convert that input to an integer and it's actually a string.
Maybe were grabbing this information from a database that stores numbers as strings and we're trying
to add something like this.
Clearly, we can't actually do this because there are two different data types.
So if I try to run this code, it says, Hey, now, instead of a syntax error, you have a type error
and it can only concatenate a string to another string, not an integer.
So we have a mismatch here.
Now this is the type of error, since it's not a syntax error that I can try to catch and do something
beforehand.
And the way this works is.
I use what's known as a tri block.
So I say, try colon and then here.
You notice it's kind of like a function where we start having an indented block of code.
Here's the block of code that I want to attempt to try.
Keep in mind, this block of code can work perfectly fine, so I can say print.
Something like hello here.
So I'm going to try to print hello.
Now let's imagine there's a mistake and this actually doesn't work.
What's the next block of code?
That is where the key word except comes into play and with just accept that's going to allow you to
actually do something if there is an exception or error raised.
So I'm going to print out.
Error.
So I'm going to save that so simple as blocks of codes or just to try something, and if there's an
exception.
Go ahead and do something here.
I'm just using print statements that are just really simple functions that are being called.
But obviously this could be way more complex blocks of code.
And often when we're actually dealing with this and jingo, I'm going to have the try block have something
to do with a user interaction.
I may be calling a function there that asks for input from a user.
Typically, if the mistake is going to happen completely internally for an old code, we're just going
to actually fix it before we put it into a try block.
So think of these try blocks as something that has some sort of user interactivity.
So I will go ahead and save that here.
And let's run this for right now.
Notice I will be able to print hello.
So I just get the output.
Hello.
The except block does not execute because we were successful in trying to print this out.
Now let's see what happens if I have something which is a mistake again.
For example, the string 10 plus the integer 10, I saved that change.
And now when I run this, I get back error.
So this is kind of interesting.
I now am able to actually report something very specific, even if I had something that previously would
have a type error.
Recall that previously when we ran this, python just said, Hey, if I see right here you have a type
error, you can only concatenate it string with a string, and it just pretty much ended the Python
code.
Now, with try and accept working in conjunction, I can actually report a custom message.
Now here's what's even more interesting I can try to accept and catch specific errors, and we can already
see that there's many different types of errors.
You can check out the documentation for all the different exception errors.
There are the for example, this specific one is called a type error.
So let's actually try to catch that.
How do we do this?
Well, what I could say is in front of, except I can accept a particular type of exception error code.
And again, you can check out the full list of built in exceptions in the documentation.
It's actually just a Google search of Python Docs exceptions.
You can check out the link in our actual file for this.
But what you do is you just put in the error type that you're expecting to catch.
So you can say accept type error and then because you know that this block of code only executes on
that specific error, I can have a more custom message reported back so I can say something like you
are using the wrong data types save that change.
And now when I run this piece of code, let me expand this a little bit here.
So when I run this piece of code after saving it, I get back the message, you're using the wrong data
types.
But what I can do is, let's say there's multiple errors that could occur.
Maybe there's an input output error.
Well, you can actually stack accept statements looking for different errors.
So there's also what's known as an i o error, which is input output.
So I could say print, you have an input output error.
And what's nice about this is later on, OK, actually, if I really know what the code is attempting
to do, I could actually pass on suggestions here so I could say something.
Did you check the file permissions or something like that so you can actually be really useful?
This is nice for when we're working off our own websites.
We could report back to the user, Hey, are you sure you passed in the correct domain because I can't
find that specific URL you're asking for?
If I'm looking for a specific error, so when you run this notice, I'm not checking for two exceptions.
I run that.
And what happens is, if it's not this exception, it continues to go down to the type error exception
and you can keep doing this.
But keep in mind, if you don't know the particular type of exception or error that's going to occur,
you can just type out, except with no particular exception code and then print whatever you want.
Hey, you got an error?
Keep in mind, it doesn't have to be a print function.
You can really execute any Python block of code there.
And it says, hey, you're using the wrong data types again, but this still works, I'm going to delete
type error.
Save.
Run this again, and it says, Hey, you got an error, so notice here I can check for particular errors
and then if there's an exception, I can print, Hey, you got an error.
What I can also do is add in an else, so I could say, try this, look for an exception and else do
something else.
So if there is no exception, then this block.
So, for example, I can say else.
Prince.
Elle Spurlock ran.
So I saved this.
Run this, and it says, Hey, you got an error, but let's actually say try 10 plus 10.
So this is actually OK.
There won't be an error here.
I run this and I get 20 and it says L Block ran.
Now, let's imagine you actually want this code to execute, even if there is an error.
So right now, I showed you a try except else.
However, else blocks are only going to run here if you don't end up having an exception.
What if you do have an exception, but also want this block of code to execute?
That's where the final block of code comes into play.
And this is typically the most common way of actually organizing errors and exception handling.
It's the block of code you want to try.
The exception error message you want to report back.
And then finally, you want to do something else.
Note that finally.
Will always run air or no air.
So it's like finally, after you attempt at all this, go ahead and do something like that.
So I'm going to save this note that right now I don't have a specific air.
In fact, I'm going to just simplify this and delete that input output error.
So I'm saying try to print 10 plus 10.
If there's an error print, hey, you got an error and then finally go ahead and execute this block
of code.
So I run this and I get back 20 because it worked.
And it says, finally, we'll always run error or no error.
Now let's institute an error.
And that would change this to be a string 10 plus 10, save those changes and run this and it says,
Hey, you got an error.
And the final block runs again.
And keep in mind if this was an else.
Like this, I wouldn't have seen that I would have run this.
And it just says, hey, you got an error if you always want this to run air or no air, then you put
in.
Finally, and this is probably the more common way of saying this, but you could use else in case you
want something else to happen.
If there is no air here, it's up to you and depending on the situation.
But typically, we're going to see things like this try and accept and then sometimes will add in a
finally as well.
And typically, the what is actually going to happen in terms of Django is you're going to attempt to
visit a URL or modify some database instructions.
And then if there is an error, we're going to report back to the user some sort of error message.
Maybe we actually put it or embedded within the actual HTML code.
And then finally, maybe we do something else, like take them to another page or refresh or something
like that.
That's what we're going to see once we know enough Django to actually do this.
But that's again.
Typically, what happens is the user tries to do something that are web sites not compatible with or
it's a mistake on the user's part.
And then we say, Hey, report back to the user.
You got some sort of error here, maybe double check what you're looking for and then take them to another
page or refresh.
Keep in mind, finally, it's technically optional here.
It will just work with a try.
Except OK, that's it.
I'll see you at the next lecture.