← VCE Software Development U3O1

Runtime error: overflow

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.

Understanding Runtime error: overflow in VCE Software Development U3O1

What it does: The if statement only allows the calculation when the exponent is small enough, so the float never goes past its maximum. A float has a maximum size (about 1.8 x 10^308). 2.0 ** 10000 is far larger, so Python raises OverflowError: (34, 'Numerical result out of range'). Overflow is about a result being too large for the storage. Dividing by zero is a different runtime error (ZeroDivisionError). Limit or validate values before the calculation (for example with a range check), or catch OverflowError with try/except so the program does not crash. The built-in exception name is OverflowError, spelled exactly and with capital letters. OverFlow is not a Python exception, so it would cause a NameError.

Common mistakes: 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 **. sys.float_info.min is the smallest positive float, not the largest. A guard against overflow needs the largest float, sys.float_info.max.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 156–166) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
In Python, what happens when you run print(2.0 ** 10000)?
It raises an OverflowError because the result is too big for a floatIt prints a perfectly accurate huge decimal number
Matches
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.
Applying
Meanings
Term
OverflowError
Meaning
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.
Practice
try:
Testing
Gaps
except :
Check Answer
Spots
1import math
2 x = 2.0 * 10000

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Badger' badge — Determined Digger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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 ✓
A float has a maximum size (about 1.8 x 10^308). 2.0 ** 10000 is far larger, so Python raises OverflowError: (34, 'Numerical result out of range').
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
Overflow is about a result being too large for the storage. Dividing by zero is a different runtime error (ZeroDivisionError).
3. Which code is likely to raise a Python OverflowError?
A. result = 2.0 ** 10000 ✓
B. result = 2 + 10000
Raising a float to a huge power goes past the float maximum. Adding two small numbers gives a small result, so there is no overflow.
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
Fixed-size integers have a maximum value. Going past it can wrap around (for example to a negative number). Python ints are different because they can grow as 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 ✓
Python integers have arbitrary size, limited only by memory. The OverflowError in this topic comes from floats, which have a fixed-size storage format.

Match the Pairs

2.0 ** 10000 → A float calculation that is too big for a float to hold, so Python raises an OverflowError
except OverflowError: → Catches the error raised when a calculation result is too big for its data type
Guard against overflow by checking the size of the exponent before doing the calculation → if power <= 1000: result = 2.0 ** power
Fixed-size integers in languages like Java or C overflow by wrapping around once they pass their maximum value → int big = 2147483647 + 1;
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.
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.

Key Term

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.

Fill in the Blanks

1. try: x = 2.0 ** 10000 except OverflowError:
Hint: Type the name of the error raised when a float calculation gets too big
2. print(2.0 ** 10000)
Hint: Type the power operator that makes the number grow far too large
3. try: big = 10.0 ** 400 except OverflowError:
Hint: Type the keyword that catches an error raised inside the try block
4. if exponent > 1000: print("Exponent too big")
Hint: Type the comparison operator that guards against a value above the limit
5. raise OverflowError("Value too big")
Hint: Type the keyword that deliberately triggers an error in your own code

Spot the Error

Question 1
A import math
B x = 2.0 * 10000
C print(x)
D except OverflowError:
E print('Too big')
Line 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 **.
Question 2
A import sys
B limit = sys.float_info.min
C x = 1.5e308
D if x > limit / 2:
E print('Too big')
Line 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.
Question 3
A import math
B n = 1000
C if n > 700:
D print(math.exp(n))
Line 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.
Question 4
A limit = 1.7e-308
B x = 1e250
C if x > limit / 1e100:
D print('Too big')
Line 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.
Question 5
A def safe_power(base, exp):
B try:
C return base ** exp
D except ZeroDivisionError:
E return None
Line D — ZeroDivisionError does not catch overflow. A result that is too big for a float raises OverflowError, so the except line must name OverflowError.

Student with a class code? Join your class →