Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
2 hours 30 minutes
Hello, everyone. And welcome back to immediate python here on Cyber eri On demand, I as always I'm your instructor, Joe Perry. And this is gonna be lesson three, the final lesson of module to in this lesson, we're talking all about exceptions in Python.
So we have three objectives were gonna learn what exceptions do what they're actually four were to learn how to use the built in exceptions and then we're gonna actually roll our own custom exception.
So fundamentally an exception is just an error message, and I talked about that a little bit in previous videos. An exception is a way to communicate to the Python interpreter that something bad has happened. Something has gone wrong. It's very often used to communicate that you've got the wrong data type or that you're out of index for list or a dictionary.
A times exception could be used to provide just information to the Python interpreter.
As you saw when we're talking about creating it, aerators and we have the raids stop exception or ray stop federation exception in general exceptions or just a way to communicate a non default state to the interpreter and they're very, very easy to use in Python. It's one of the many things that I love about this language. So we're gonna go ahead. We're gonna do
except demo dot pie
user been pylon three
There you go.
And we're just gonna write a very simple function here that's going to do something wrong so you can see that it's we can see it spit out an exception. We can start to work with us. We're gonna say Death main
a very common error that we see and off by one error. We're gonna implement that right here. This four I in range six
at the index.
So we know that this isn't going to work, right? We know this is gonna spit out an error for us because we've seen it happen before. Not only in your own good, but in some of my own videos. So we're gonna go ahead. We're gonna run this. We're going to see what happens.
There we go.
And you can see here. We have a trace back. We have an index error down. At the bottom of this trace back is a wave of Pythons interpreter to tell you what function you were in when the mistake happened. When the problem happened, you can see here that it starts says you're in the main function, and then inside of that you're in a print function
that indicates that's where the mistake happened. It's sort of pythons built in way to help you identify
which stack from urine and therefore, how did most easily identify and fix the problem. But importantly to this lesson, we concede on here at the bottom, it spit out an index error, so that's what we have to handle. So when you're working in Python, a very common
format you're going to senior code is what's called a try except block. And what a try block is is just a way to attempt some chunk of code. And if an exception get spit out, you could potentially handle that exception. So the way we would implement that here
because we do try
and then, as always in python, will time everything that belongs to the tribe, lock into
it's proper location,
and then we can do. Except now there are two different ways that you could do an except statement you can simply do it as I have here,
or you could do except
a specific exception type
and the reason why you want to do. And almost always you're gonna want to write a specific exception type of. The reason why you want to do that is because you don't want to silently kill a different kind of exception. Remember way, way back in the beginners python, the very first python video we talked about that the Zen of Python. The idea of
one of the core concept one, the court aphorisms of this language is that error should never be silent unless they're explicitly silenced. So here we can see we haven't except Index air, because that is a that is an anticipated exception, and we can write a code black here that will handle that.
If, on the other hand, we were, too,
just do an except with no specific exception attached to it, we might have an index error, but we might have any other kind of error that could potentially crop up in the execution of this code. And because we're we're silencing everything without any introspection, we don't know anything about what's happening. We're going to lose a lot of relevant and important information.
So you always want to use the most specific except statement that you can,
and in this case, I'm going to do except index error. And I'm just going to friends
Index out of now. That might not seem very useful right now, because obviously we're going to stop execution either way. Either we're gonna get that exception is gonna kill the program, or we're going to print that the indexes out of rage and we're gonna kill the program. One of the reasons why try and accept is such a useful designed to the language
is because of the idea that
instead of the program dying, if some chunk of code goes wrong, you can have these statements built into your program in various places that you know, our error prone so that even if an error comes up, even if something goes wrong with that chunk of code, your whole program doesn't have to die on a trivial program like this. You don't necessarily see it as much.
But when you're writing a larger partner when you're dealing with your network handling or some
you know, massive, Siri's of functions. This is a really useful code block so that if something goes wrong, everything doesn't have to. So we can see here what will happen. Remember that last time it printed out an exception and everything went crazy. Now you accept demo and you can see that we'll just print the index is out of range.
And if there were code after that cry except it would execute just fine. So if we were to do
something like this, we can see
definitional, we concede, definitely rather that we managed to control our execution, get out of the error, even though still created that exception, we're now safely handling it.
That's how you use the built in python exceptions there dozens and dozens of built in exceptions or custom exceptions that you'll get when you import modules. What we want to talk about now very briefly is how you can actually roll your own, how you can create your own exceptions on the way we're gonna do. That is where we're gonna go back in there except except demo,
and we're going to remember something that's been very relevant for a few videos now, which is that everything in Python is secretly a class. Everything is implemented as a class, and the specific class we're going to be using here is just based on the exception class. So what we're gonna do is we're gonna create class
and remember that when we're invoking a previous type of class, we're we're inheriting from apparent class. We create our class with parentheses and call that and we use that parent class like an argument
And here we'll just do
you could do all sorts of really fascinating and interesting things. With this, you can build an actual functionality into an exception because again, this is a class. So you could do anything with this. You could do with a normal class, But pretty often you'll see custom exceptions like this just rolled with just the exception and a print statement. It's a pretty simple, just a way of
sort of demonstrating. Hey, something went wrong. Don't know what, but this is the problem.
So let's have a look here and see how this works
down here in Maine. We're gonna go ahead and delete some lines of code real fast,
and we're just gonna do ourselves a trivial example before I in range,
and let's see what happens here.
All right, you can see Fascinatingly. Not only did it print our exception found statement, but we got the same explosion that we got when we use the built in exceptions. And that's really important, Understand is that when you're creating your custom exceptions here, you still have to handle them
exactly the same way you would have to handle any built. An exception. There's no there's no special treatment that you get because this is custom. There's no special handling that you get.
You have to do your try, and except when you're raising your own custom exceptions. So that's all there is for this video. Again, we talked about what exceptions are used to do in Python, how we can use them, what they're functionally capable of. We learned how to use the built in exceptions pretty easily pretty straightforward, and then we roll our own custom exception.
As with all of the videos, I recommend you take a little bit of time and work through some of our lab assignment so that you can get very familiar with this.
But once you come back, we're gonna do a summary written summary and review because you have now officially completed the second module out of three here in Intermediate Python. And I have great news because the third module is very light, very easy compared to some of the stuff that we've been learning about classes. It's gonna be a cakewalk.
So thank you all for watching. I really appreciate everyone coming out and checking out these videos. Hopefully, you're learning a lot. Hopefully, you're getting more familiar with Python, and this video made you comfortable. With exceptions. As always, I have been your instructor, Joe Perry, and you're watching Intermediate Python on Cyber Eri on demand.
Course Assessment - Intermediate Python