Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Python error handling
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's see the error handling here in Python. First of all, we're generating the same set of values.
0:05
We're getting a list out of this first 1 to 20 numbers and then we're adding on these additional values here like so. So really nice
0:14
and we're just doing one line here instead of 4 or 5. We're going to loop over those values. I'm going to call this sketchy_method.
0:21
In the moment, we're not doing any error handling whatsoever so you can imagine this might not go well. Also notice up at the top
0:28
we have something called colorama and this allows us to set the foreground color and so on for our output
0:36
just like we were console foreground and whatnot. So in order to install this we have to install this separately. There's a couple of things we can do.
0:44
We come over here and have a new text file which is a convention in Python. We'll talk more about it later but just so we have this listed somewhere.
0:55
So we have something called requirements.txt and the tooling knows about it. So if I go over here and just I put colorama
1:01
notice immediately, PyCharm is like whoa, whoa, whoa. Colorama is not satisfied. We need to install this. Where is it going to install?
1:08
It's going to install it into our whatever virtual environment is active there. So it's going to install to this one. So we could do that manually
1:18
or we could let PyCharm do it. So I'll go ahead and just let PyCharm do it. Boom, it's installed. It's still got an underline
1:25
but that's just 'cause it's misspelled. I'm going to say no, it's not misspelled. We come back here, now this works, okay?
1:32
So now we can do, like, Fore.yellow, blue, whatever. All right, so let's run this program here and see what we get.
1:40
Well, it worked for a little while 2, 3, 4, 5 and then it looks like the error happened earlier but it actually happened at the very end.
1:47
It's just the priority of the system error output stream is higher than the priority of just the regular output. So it comes out of order, right
1:57
if we don't flush things and so on but ignore that, right? It ran for a while until it got to 6 but it didn't actually work.
2:04
It crashed and we got an ArithmeticError. Okay, so let's start adding in the type of error handling we had before. Remember with C#
2:15
we had try and catch this, catch that. So Python has almost the same syntax, try do something. You don't say catch. You say except, like this.
2:28
You can just have it blank. That probably isn't what you want. But we could print out oops, like this. And let's make it some kind of color.
2:37
Foreground.red, light red, let's say. Now if you run it you can see oops, oops, periodically. It's crashing. But we can do better. Just like in C#
2:46
we were able to capture these different types of errors we can say except error type. Instead of catch error type, except error type.
2:54
Now what we can do is we can put out some message about the type of error that we got. And we could do something like our value here
3:04
and maybe let's make it really obvious that this is an error, like that. Okay, try again. Cannot compute with 18 because it was an ArithmeticError
3:13
and we got another one which is a ValueError. So we're going to get some of these random types and we've done the studying of this sketchy_method
3:20
and we've realized that there's a couple of types we can catch types of errors we can deal with. There's a BrokenPipeError
3:27
and this'll be check your network. Check your wifi, okay? We could get an ArithmeticError and then there may be other ones that we're not aware of.
3:40
So down here we can just say except Exception and we can print out, let me just copy this. Print out something like this.
3:48
So when we didn't know what we were doing before like when we didn't know what the error type was we said error and we have the type name
3:55
and then the actual problem. So in order to do that we actually need to capture some sort of variables. So you don't say exception ex.
4:05
You would say as ex, right? This is how it works. And what is this unhappy about? It just says too broad. It wants us to catch more specific stuff.
4:14
Alright, so now we come over and we could print the same type of thing. So we'd say give me the type of ex. And under name, like this
4:23
that's the type name there and then we could have some kind of thing like what is the error. Just the string that's
4:29
this is the way you do to string on an object in Python. And let's space it out a little and run it now. Nice, look at that.
4:38
So we got exactly the same error. We got a cannot compute a 6 and a network error cannot compute with 12. Oh, let's see down here what we have.
4:48
Oh, this is one that was unknown. It's a ValueError. None apparently is not valid. So when we didn't specifically handle this
4:56
as an exception case we were able to catch it and do something kind of meaningful with it, I guess. And then the rest of it, that worked.
5:04
Pretty cool, right? This is actually super, super similar to C#. Let's go and do this side by side again here.
5:10
Oh, that whole bit of screen there is the C# one. From here to there, that's the Python one. We can see it's the same basic structure
5:17
for each, or for in and then try do the block. Try do the block. And then either except exception type or catch exception type
5:27
and then you just deal with it. If you want a variable, the you define the variable either like this here or as ex over here. Beautiful.
5:35
This should be really, really familiar to you there if you've worked with error handling in C# it should be super comfortable.
5:41
And there's also a finally in both. Obviously there's a finally in C# that you probably know. There's a try finally so we could add a final block here.
5:50
Finally, you know, one colon, print. Finally, whew. So you can see there's a bunch of finallys coming out now.
5:58
Alright. I'm going to comment that out 'cause it kind of messes it up but I'll leave it in there as a comment.
6:05
So try except finally versus try catch finally.