Runtime error: overflow

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Runtime error: overflow

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In Python, what happens when you run print(2.0 ** 10000)?
A It prints a perfectly accurate huge decimal number
B It raises an OverflowError because the result is too big for a float
2. Which statement best describes an overflow error?
A A calculation gives a number too big for its data type or storage to hold
B A calculation tries to divide a number by zero
3. Which code is likely to raise a Python OverflowError?
A result = 2.0 ** 10000
B result = 2 + 10000
4. In languages like C or Java, an int is stored in a fixed number of bits. What can happen if a calculation goes past the largest value it can hold?
A The value overflows and may wrap around to a wrong number
B The int automatically grows to any size needed
5. True or false: Python's int type has a fixed maximum, so 2 ** 10000 (both ints) always raises an OverflowError.
A True: Python ints stop at a fixed maximum
B False: Python ints can grow to hold very large values

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
12.0 ** 10000 AA float calculation that is too big for a float to hold, so Python raises an OverflowError
2except OverflowError: Bif power <= 1000: result = 2.0 ** power
3Guard against overflow by checking the size of the exponent before doing the calculation CThere is no check or try/except, so the overflow stops the program with an OverflowError. Guard the value or wrap the calculation in try/except.
4Fixed-size integers in languages like Java or C overflow by wrapping around once they pass their maximum value Dint big = 2147483647 + 1;
5result = 2.0 ** 10000 print(result) EThe wrong error type is caught. An overflow raises OverflowError, not ValueError, so the program still crashes. Use except OverflowError.
6try: x = 2.0 ** 10000 except ValueError: FCatches the error raised when a calculation result is too big for its data type

Definitions

OverflowError

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
try: x = 2.0 ** 10000 except _____________:
2.
print(2.0 __ 10000)
3.
try: big = 10.0 ** 400 ______ OverflowError:
4.
if exponent _ 1000: print("Exponent too big")
5.
_____ OverflowError("Value too big")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import math
B x = 2.0 * 10000
C print(x)
D except OverflowError:
E print('Too big')
Question 2
A import sys
B limit = sys.float_info.min
C x = 1.5e308
D if x > limit / 2:
E print('Too big')
Question 3
A import math
B n = 1000
C if n > 700:
D print(math.exp(n))
Question 4
A limit = 1.7e-308
B x = 1e250
C if x > limit / 1e100:
D print('Too big')
Question 5
A def safe_power(base, exp):
B try:
C return base ** exp
D except ZeroDivisionError:
E return None

Answer Key

Runtime error: overflow
Section A: Quick Questions
Q1
B — It raises an OverflowError because the result is too big for a float
Q2
A — A calculation gives a number too big for its data type or storage to hold
Q3
A — result = 2.0 ** 10000
Q4
A — The value overflows and may wrap around to a wrong number
Q5
B — False: Python ints can grow to hold very large values
Section B: Match the Pairs
Q1
2.0 ** 10000 → A float calculation that is too big for a float to hold, so Python raises an OverflowError
Q2
except OverflowError: → Catches the error raised when a calculation result is too big for its data type
Q3
Guard against overflow by checking the size of the exponent before doing the calculation → if power <= 1000: result = 2.0 ** power
Q4
Fixed-size integers in languages like Java or C overflow by wrapping around once they pass their maximum value → int big = 2147483647 + 1;
Q5
result = 2.0 ** 10000 print(result) → There is no check or try/except, so the overflow stops the program with an OverflowError. Guard the value or wrap the calculation in try/except.
Q6
try: x = 2.0 ** 10000 except ValueError: → The wrong error type is caught. An overflow raises OverflowError, not ValueError, so the program still crashes. Use except OverflowError.
Section C: Definitions
Q1
OverflowError — An error raised when a calculation gives a number too big for its data type to hold, such as a Python float going past its maximum, e.g. 2.0 ** 10000.
Section D: Fill in the Blanks
Q1
try: x = 2.0 ** 10000 except OverflowError:
Q2
print(2.0 ** 10000)
Q3
try: big = 10.0 ** 400 except OverflowError:
Q4
if exponent > 1000: print("Exponent too big")
Q5
raise OverflowError("Value too big")
Section E: Spot the Error
Q1
B — This is wrong: the program should start with try: and use the ** power operator. Multiplying 2.0 * 10000 gives 20000.0, which fits easily in a float. 2.0 ** 10000 is too big for a float, so it raises OverflowError. Note that the first line should also be try:, but the error to fix here is * instead of **.
Q2
B — sys.float_info.min is the smallest positive float, not the largest. A guard against overflow needs the largest float, sys.float_info.max.
Q3
C — The guard is the wrong way round. math.exp(n) is safe only when n is small (below about 709), so the check must be n < 700. With n > 700 the program calls math.exp(1000), which raises OverflowError.
Q4
A — The exponent has the wrong sign. 1.7e-308 is a tiny number close to zero. The float maximum is about 1.7e308, so the limit must be 1.7e308.
Q5
D — ZeroDivisionError does not catch overflow. A result that is too big for a float raises OverflowError, so the except line must name OverflowError.