6. Re-raise exceptions deliberately
Catch specific exceptions and re-raise with context. Avoid swallowing errors or using a bare except.
Re-raising deliberately keeps the failure visible while adding just enough local context to explain what the code was trying to do. That usually produces better debugging information than either swallowing the error or replacing it with a vague new one.
6.1. Don’t do this
1try:
2 config = load_config('settings.json')
3except:
4 raise RuntimeError('configuration failed')
6.2. Do this
1try:
2 config = load_config('settings.json')
3except FileNotFoundError as exc:
4 raise RuntimeError('settings.json was not found') from exc