Skip to content

python.bare_except

Correctness Medium

Detects except: without an exception type, which catches all exceptions including KeyboardInterrupt and SystemExit.

Bare except: catches everything, not just the errors you intended:

  • KeyboardInterrupt — Ctrl+C won’t stop your program
  • SystemExitsys.exit() calls get swallowed
  • MemoryError — System is out of memory but code continues
  • Real bugs — Typos and logic errors get hidden

This makes programs hard to stop and hides real issues that should crash loudly.

# ❌ Before
try:
risky_operation()
except:
pass

If risky_operation has a bug, you’ll never know. If the user presses Ctrl+C, nothing happens.

# ✅ After
try:
risky_operation()
except Exception as e:
logger.error("Operation failed", exc_info=e)

Exception excludes system-level errors like KeyboardInterrupt. The error is logged instead of silently swallowed.

  • except: without any exception class
  • except: followed by pass (the worst pattern)

Unfault converts except: to except Exception as e: and adds logging if the body was empty or just pass.