sys.exit

Learn how to use Sentry to capture sys.exit calls.

The SysExitIntegration records sys.exit calls by capturing the SystemExit exception raised by sys.exit.

Copied
pip install --upgrade "sentry-sdk"

The SysExitIntegration is disabled by default, and the SDK only captures SystemExit exceptions automatically if this integration is manually enabled.

A sys.exit call is considered "successful" when the function is passed a value of 0 or None. Passing any other value results in an "unsuccessful" exit.

To enable the SysExitIntegration and configure it to only capture unsuccessful sys.exit calls, use the following:

Copied
import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0
example-org / example-project
"
,
# Add data like request headers and IP for users, if applicable; # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info send_default_pii=True, integrations=[SysExitIntegration()], )

If you wish to capture successful and unsuccessful sys.exit calls, use the following, instead:

Copied
import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0
example-org / example-project
"
,
integrations=[SysExitIntegration(capture_successful_exits=True)], )

Running the following snippet should result in a SystemExit exception being reported to Sentry.

Copied
import sys

import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0
example-org / example-project
"
,
integrations=[SysExitIntegration()], ) sys.exit(1)

If you use capture_successful_exits=True, calling sys.exit(0) should also report a SystemExit exception to Sentry.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").