Python MemoryError Exception
Example
A MemoryError occurs if a program tries to use more memory than the computer has:
x = "a" * 10**18
Try it Yourself »
Definition and Usage
The MemoryError exception occurs when Python runs out of memory.
In the example, the string would need 1018 bytes (one million terabytes) of memory.
Exception Handling
Example
Handling the MemoryError in a try...except statement:
try:
x = "a" * 10**18
except MemoryError:
print("There is not enough memory")
Try it Yourself »
Related Pages
Read more about handling errors in our Python Try Except Tutorial.