Python Glossary Русский

Python 3.5+Exceptionsexception

RecursionError

built-in exceptions

Raised when the maximum recursion depth (1000 by default) is exceeded. A subclass of RuntimeError.

Syntax

raise RecursionError('...')

Nine times out of ten the cause is a missing or unreachable base case, not a limit that is too low; raising sys.setrecursionlimit() is safe only when the recursion is genuinely finite, otherwise the process crashes for real, past the point where except can help. The limit covers the whole interpreter stack, so frames from library calls eat into the same budget as yours. Being a RuntimeError subclass, it is also swallowed by a broad except RuntimeError.

Examples

def infinite():
    return infinite()
try:
    infinite()
except RecursionError:
    print('слишком глубокая рекурсия')
    import sys
    sys.setrecursionlimit(2000)  # изменить лимит

See also

Open in the full glossarydocs.python.orgReport an inaccuracy