Python Glossary Русский

all Python 3Exceptionsexception

SystemExit

built-in exceptions

Raised by sys.exit() to terminate the program. Inherits BaseException.

Syntax

raise SystemExit

It inherits from BaseException rather than Exception, which is exactly why a normal except Exception lets it pass and sys.exit() actually reaches the interpreter. A bare except: or except BaseException, on the other hand, swallows it and the program keeps running — the usual reason people complain that sys.exit() does nothing. Whatever you pass to sys.exit() ends up in the code attribute: an integer becomes the exit status, a string is printed to stderr and the status becomes 1.

Examples

try:
    raise SystemExit
except SystemExit as e:
    print(type(e).__name__)   # → SystemExit

See also

Open in the full glossarydocs.python.orgReport an inaccuracy