3.5 Exceptions
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
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.
00:14
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.
00:23
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.
00:40
A times exception could be used to provide just information to the Python interpreter.
00:45
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
01:03
them
01:04
except demo dot pie
01:10
user been pylon three
01:12
python three.
01:15
There you go.
01:15
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
01:26
list one
01:36
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
01:45
print
01:47
print
01:48
list, one
01:51
at the index.
01:53
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.
02:10
Nope.
02:13
There we go.
02:15
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
02:32
that indicates that's where the mistake happened. It's sort of pythons built in way to help you identify
02:38
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
02:53
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
03:10
because we do try
03:13
and then, as always in python, will time everything that belongs to the tribe, lock into
03:17
it's proper location,
03:20
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,
03:27
or you could do except
03:30
a specific exception type
03:34
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
03:53
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.
04:11
If, on the other hand, we were, too,
04:14
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.
04:32
So you always want to use the most specific except statement that you can,
04:36
and in this case, I'm going to do except index error. And I'm just going to friends
04:41
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
05:00
is because of the idea that
05:02
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.
05:20
But when you're writing a larger partner when you're dealing with your network handling or some
05:24
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.
05:41
And if there were code after that cry except it would execute just fine. So if we were to do
05:58
something like this, we can see
06:00
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.
06:12
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,
06:30
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
06:47
custom exception
06:49
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
07:00
exception.
07:03
And here we'll just do
07:08
Prince
07:10
exception found
07:13
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
07:32
sort of demonstrating. Hey, something went wrong. Don't know what, but this is the problem.
07:39
So let's have a look here and see how this works
07:43
down here in Maine. We're gonna go ahead and delete some lines of code real fast,
07:48
and we're just gonna do ourselves a trivial example before I in range,
07:55
I can
07:56
if I
07:58
module Oh
08:03
three
08:05
zero,
08:05
raise
08:07
custom exception
08:11
and let's see what happens here.
08:18
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
08:31
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.
08:39
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.
08:56
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.
09:03
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.
09:22
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.
Up Next
Course Assessment - Intermediate Python
Assessment
30m
Similar Content