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.

Install

Copied
pip install "sentry-sdk"

Configure

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="___PUBLIC_DSN___",
    # 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="___PUBLIC_DSN___",
    integrations=[SysExitIntegration(capture_successful_exits=True)],
)

Verify

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="___PUBLIC_DSN___",
    integrations=[SysExitIntegration()],
)

sys.exit(1)

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

Manually-raised SystemExit

Please note that the SysExitIntegration only captures SystemExit exceptions which are raised by calling sys.exit. If your code raises SystemExit without calling sys.exit, the exception will not be reported 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").