Home

Systemexit

SystemExit is a built-in exception in Python that signals the interpreter to terminate execution. It is a subclass of BaseException and is raised either explicitly by the programmer via raise SystemExit(code) or implicitly by the standard library function sys.exit([code]).

Code semantics: When SystemExit is raised, the interpreter unwinds the call stack, runs finally blocks, and

Catching: SystemExit can be caught with a try/except block, using except SystemExit as e. In that case,

Relationship and usage: SystemExit is the conventional mechanism for terminating a Python program, distinct from signals

See also: sys.exit, KeyboardInterrupt, BaseException.

then
exits.
If
SystemExit
is
not
caught
by
user
code,
the
interpreter
terminates
with
an
exit
status
determined
by
the
code
attribute.
If
code
is
None,
the
exit
status
is
0.
If
code
is
an
integer,
that
value
is
used
as
the
exit
status.
If
code
is
a
string,
the
string
is
written
to
standard
error
and
the
exit
status
is
1.
If
code
is
another
exception
instance,
its
string
representation
is
printed
and
the
exit
status
is
1.
the
code
can
be
inspected
via
e.code,
and
the
program
may
choose
to
suppress
termination
or
re-raise
the
exception.
Because
SystemExit
derives
from
BaseException
rather
than
Exception,
a
bare
except:
clause
will
not
catch
it;
thus
catching
SystemExit
typically
requires
an
explicit
except
SystemExit.
or
OS-level
termination.
It
allows
a
program
to
signal
a
controlled
exit
and
provides
a
payload
that
can
be
inspected
by
surrounding
code
if
needed.