Dealing With Arithmetic Error Exceptions
00:00
When you need to crunch numbers, Python is there for you, but exceptional situations can occur anywhere. And Python provides a few built-in exceptions, specifically related to arithmetic operations, and each of these is a subclass of the ArithmeticError
class.
00:14
You have the FloatingPointError
, which while it is defined as a built-in exception, isn’t used in modern Python, so we won’t discuss it further here.
00:22
The ZeroDivisionError
by far, the most common of these errors and the OverflowError
, which while rare you should still be aware of.
00:36
A ZeroDivisionError
is raised whenever the right-hand side of a division operation is zero. And it’s surprising how commonly that can happen.
00:44
Here’s an example: def average_grade(
grades):
return sum(grades)
divided by len(grades)
. Here you have a function to calculate the average grade from a list of grades.
00:59 Try it out by passing in a list of the integers 4, 3, 3, 4, and 5.
01:07 It’s 3.80. That looks right to me. But what if you passed an empty list?
01:14
ZeroDivisionError: division by zero
. Yes. This is because the length of the grades list in this case is zero. The error tells you what happened, but it’s still not very descriptive.
01:26
To the user of the function, it might not even be obvious how this error could happen. You can improve your function by catching the error and raising one of your own with a clearer message, like this: def average_grade(grades):
try return sum(grades) / len(grades)
except ZeroDivisionError
01:50
raise ValueError
passing in the string: “You should pass at least one grade to average_grade()”.
02:02
from None
. By placing the original logic in the try
block, the function behaves the same when no error occurs. And when you catch the ZeroDivisionError
, you instead raise a ValueError
because it’s actually the values in the list or lack of values that are the problem.
02:20
And let’s try it out. Call average_grade()
passing in an empty list: ValueError:
"you should pass at least one grade to average_grade()"
. Perfect.
02:30
And this is a much better experience for the user. Another thing to note is that a ZeroDivisionError
will also occur if you try to take the modulo of a number by zero, like 42 %
0
, ZeroDivisionError: integer division or modulo by zero
.
02:47
So that’s one more thing to watch out for. And finally, though they are rare, an OverflowError
is raised when the float result of an arithmetic operation exceeds the maximum representable value for a float.
03:01
Like if you try to raise the float 10.0**1000
03:07
OverflowError: (34, 'Result too large')
. It’s rare to run into, but if you do, you know what’s causing it. And generally, this will only happen with floats not integers because Python supports integers of arbitrary size as long as they fit within the available memory.
03:25 And that’s all the math we’ll have in this course. Next up, the exceptions you need to know when handling files with Python.
Become a Member to join the conversation.