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