---
title: Never Use Bare except Clause
impact: HIGH
impactDescription: A bare except catches SystemExit and KeyboardInterrupt, making programs impossible to interrupt and hiding unrelated errors that should propagate.
tags: python, exceptions, error-handling, pitfalls, quality
---

## Never Use Bare except Clause

A bare `except:` clause (with no exception type specified) catches **every** exception, including `SystemExit` (raised by `sys.exit()`), `KeyboardInterrupt` (Ctrl+C), and `GeneratorExit`. This prevents programs from being terminated normally, swallows programming errors, and makes debugging extremely difficult.

Always specify the exception type(s) you intend to handle.

**Incorrect:**
```python
# Catches SystemExit and KeyboardInterrupt — program cannot be stopped
try:
    process_data()
except:
    print("Something went wrong")

# Also problematic: too broad
try:
    connect_to_db()
except Exception:
    pass  # silently ignores all errors including programming mistakes
```

**Correct:**
```python
# Catch only what you expect and can handle
try:
    process_data()
except ValueError as e:
    logger.warning("Invalid data: %s", e)
except OSError as e:
    logger.error("I/O error during processing: %s", e)

# If you need a catch-all, at least log it and re-raise
try:
    connect_to_db()
except Exception as e:
    logger.error("Unexpected error connecting to DB: %s", e, exc_info=True)
    raise

# Correct way to suppress a specific expected error
import contextlib

with contextlib.suppress(FileNotFoundError):
    os.remove("temp.txt")
```

**Exception hierarchy to catch:**
```python
# Prefer specific → broad order
try:
    result = int(user_input)
except ValueError:
    result = 0  # handle invalid literal
except (TypeError, OverflowError) as e:
    logger.warning("Type issue: %s", e)
    result = 0
```

**Tools:** Ruff `E722` (bare-except), `W0702` in Pylint, `BLE001` (blind-except), flake8, pyflakes
