Built-in Exceptions

MemoryError — exception class

Indicates that an allocation could not be satisfied. Recovery is not always possible; free resources if safe and avoid allocating large diagnostic objects. Do not deliberately exhaust system memory to test a handler. The example raises a controlled stand-in to demonstrate handling; it does not cause the real system failure. Catch MemoryError when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
MemoryError

Return Value

type

The class object MemoryError; this reference documents the class identity, not a universal constructor signature.

Working Example

Example Snippetpython
try:    raise MemoryError("simulated allocation failure")except MemoryError as error:    print(type(error).__name__)
Python Reference