python.bare_except
Correctness
Medium
Detects except: without an exception type, which catches all exceptions including KeyboardInterrupt and SystemExit.
Why It Matters
Section titled “Why It Matters”Bare except: catches everything, not just the errors you intended:
KeyboardInterrupt— Ctrl+C won’t stop your programSystemExit—sys.exit()calls get swallowedMemoryError— 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.
Example
Section titled “Example”# ❌ Beforetry: risky_operation()except: passIf risky_operation has a bug, you’ll never know. If the user presses Ctrl+C, nothing happens.
# ✅ Aftertry: 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.
What Unfault Detects
Section titled “What Unfault Detects”except:without any exception classexcept:followed bypass(the worst pattern)
Auto-Fix
Section titled “Auto-Fix”Unfault converts except: to except Exception as e: and adds logging if the body was empty or just pass.