OverflowError

OverflowError is a built-in exception that occurs when the result of an arithmetic operation is too large to be expressed within the available numeric type’s range.

Python’s integers automatically expand to accommodate larger values. In practice, attempting to create extremely large integers may lead to a MemoryError if the system cannot allocate enough memory. However, for historical reasons, older Python code may sometimes raise OverflowError for integers that are outside a required range.

Because floating-point numbers have a fixed range, OverflowError is more commonly associated with floating-point operations. When a calculation exceeds the fixed range, Python raises an OverflowError.

OverflowError Occurs When

  • Performing mathematical calculations that result in extremely large numbers
  • Working with floating-point operations that exceed the platform’s floating-point number range

OverflowError Can Be Used When

Using libraries or functions that perform intensive numerical computations

OverflowError Example

An example of when the exception appears during a floating-point overflow:

Python
>>> 10.0**1000
Traceback (most recent call last):
    ...
OverflowError: (34, 'Result too large')

An example of how to handle the exception:

Python
>>> try:
...     result = 10.0**1000
... except OverflowError:
...     result = float('inf')  # Assign infinity to handle overflow
...     print(result)
...
inf

Tutorial

Python's Built-in Exceptions: A Walkthrough With Examples

In this tutorial, you'll get to know some of the most commonly used built-in exceptions in Python. You'll learn when these exceptions can appear in your code and how to handle them. Finally, you'll learn how to raise some of these exceptions in your code.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 17, 2025 • Reviewed by Martin Breuss