# Blind except
Overly broad except clauses can lead to unexpected behavior, such as catching `KeyboardInterrupt` or `SystemExit` exceptions that make it harder to interrupt a program with Control-C, and can disguise other problems. [^1]
```python
# Don't
try:
foo()
except:
...
# Or
try:
foo()
except BaseException:
...
# Or
try:
foo()
except Exception:
...
```
```python
# Do
try:
foo()
except FileNotFoundError:
...
# Or perform logging (recommended)
try:
foo()
finally:
... # e.g. perform cleanup here, propagation will occure
# Or perform logging (alternative)
try:
foo()
except BaseException:
... # e.g. perform cleanup here before propagating
raise
# Or
try:
foo()
except BaseException:
# printing out or logging the traceback making the user aware
# that an error has occurred.
logging.exception("Something went wrong")
```
## Detection
This anti-pattern can be detected by [[Python static code analyzer|Python static code analyzers]] such as [[Ruff]] and [[Flake8]] extension:
### Configuration
#### Ruff (recommended)
##### Config file
Using [[pyproject.toml]] or [[ruff.toml]] file.
For the `flake8-blind-except`category: [^2]
```toml
[tool.ruff.lint]
select = [..., "BLE", ...]
```
For the specific `blind-except` rule: [^3]
```toml
[tool.ruff.lint]
select = [..., "BLE001", ...]
```
##### CLI
It is possible to configure the `flake8-blind-except` rule using [[Ruff]] [[CLI - Command Line Interface|CLI]] options.
For the `flake8-blind-except`category: [^2]
```sh
ruff check --select BLE path/to/code/
```
For the specific `blind-except` rule: [^3]
```sh
ruff check --select BLE001 path/to/code/
```
#### Flake8
There is no configuration available for `flake8-blind-except` except having the extension installed: [^4]
```sh
$ pip install flake8-blind-except
```
---
[^1]: [PEP 8 – Style Guide for Python Code#programming-recommendations - peps.python.org](https://peps.python.org/pep-0008/#programming-recommendations)
[^2]: [flake8-blind-except (BLE) - docs.astral.sh](https://docs.astral.sh/ruff/rules/#flake8-blind-except-ble)
[^3]: [blind-except (BLE001) - docs.astral.sh](https://docs.astral.sh/ruff/rules/blind-except/)
[^4]: [flake8-blind-except - github.com](https://github.com/elijahandrews/flake8-blind-except)