.. _bare-except:

bare-except / W0702
===================

**Message emitted:**

No exception type(s) specified

**Description:**

*A bare ``except:`` clause will catch ``SystemExit`` and ``KeyboardInterrupt`` exceptions, making it harder to interrupt a program with ``Control-C``, and can disguise other problems. If you want to catch all exceptions that signal program errors, use ``except Exception:`` (bare except is equivalent to ``except BaseException:``).*

**Problematic code:**

.. literalinclude:: /data/messages/b/bare-except/bad.py
   :language: python

**Correct code:**

.. literalinclude:: /data/messages/b/bare-except/good.py
   :language: python

**Additional details:**

A good rule of thumb is to limit use of bare ‘except’ clauses to two cases:
- If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred.
- If the code needs to do some cleanup work, but then lets the exception propagate upwards with raise. ``try...finally`` can be a better way to handle this case.

**Related links:**

- `Programming recommendation in PEP8 <https://peps.python.org/pep-0008/#programming-recommendations>`_

Created by the `exceptions <https://github.com/PyCQA/pylint/blob/main/pylint/checkers/exceptions.py>`__ checker.