Check out a free preview of the full Intermediate Python course

The "Error Specificity" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates why error order matters. Students are also warned against "swallowing the exception".

Preview
Close

Transcript from the "Error Specificity" Lesson

[00:00:00]
>> Nina Zakharenko: We want to catch the more specific exception first.
>> Nina Zakharenko: So I'm going to make a little function here, and I'm gonna call it division. And it's gonna take the number to divide, Sam and I just call that num. And I’m gonna say, try: result =, let me make sure my indentation is correct here.

[00:00:33]
Okay, result is going to equal to the value of pi divided by the user input number. We're going to except on ArithmeticError and print out that we had a general math error. Otherwise, I'm also going to accept on a special type of exception called the ZeroDivisionError. Why might this come up?

[00:01:08]
>> Speaker 2: If you divide by zero and the number is zero.
>> Nina Zakharenko: Yes, so if the number is zero. Yeah, and I can say, Divide by zero error.
>> Speaker 3: I think you're gonna have problems because the arithmetic is spelled wrong and you missed the first i, and so-
>> Nina Zakharenko: Thank you.

[00:01:29]
Let me just put that back in real quickly. So result = 3.14 / num and except Arithmetic. Got that right. Yeah, Error. Okay, and I'm gonna print out, math error.
>> Nina Zakharenko: And then I'm gonna except on ZeroDivisionError. And I'm going to print, zero division. Okay, if I try to do division by 0, we're gonna see something a little bit unexpected.

[00:02:14]
I'm going to get a math error, and that's because the 0 division error is the more specific error, and arithmetic error is the more general one. So by catching the more general exception first, I swallowed the more helpful error message. And to figure out this exception hierarchy, you can look at the Python docs page.

[00:02:44]
>> Nina Zakharenko: And check it out. There's a lot there, the link is in the course website. In general, it's pretty bad to catch exception in your code, because let's say I was using the GitHub API, and I caught exception when I tried to use it. I just completely lost context of what the general exception was.

[00:03:14]
And now I can't really change the flow of my program by what I can do with it. The absolute worst thing that you can do is try some code.
>> Nina Zakharenko: Except on Exception.
>> Nina Zakharenko: And then do nothing.
>> Nina Zakharenko: Everything went fine according to my code, right? So this is called swallowing the exception.

[00:03:48]
You should at least try to log it or do something, maybe page your ops guy depending on the severity of the error. But by doing this you'll really lose a lot of context. And again, generally don't catch base exception, or not generally, but definitely don't do that ever, ever.

[00:04:09]
That's even the worst form of this.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now