Skip to content

Python · correctness

no-sentinel-return-on-except

python:no-sentinel-return-on-except

Exception handler silently converts a failure into a sentinel return value.

Code
SARJ009
Default
error
Fix
none
Languages
python

Why

Unobservable sentinel returns erase failure context and make error handling ambiguous for callers.

Fix

Re-raise the exception, log it before returning, or expose failure through a typed result.

Before / after

Executed by this rule’s unit tests.

Before

Exception silently converted to None

service.py · focus
def load():
    try:
        return risky()
    except Exception:
        return None

After

Exception logged before returning None

service.py · focus
def load():
    try:
        return risky()
    except Exception:
        logger.warning('load failed')
        return None

Limits

  • Only final empty or false sentinel returns and bare `except: pass` handlers are reported.
  • Handlers that re-raise, log, print the exception, or implement a recognized result contract are excluded.