Skip to content

Commit

Permalink
python/style_guide: Add bullet about custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Oct 18, 2023
1 parent b665366 commit e309ed4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions docs/python/style_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ Type hints are especially useful in :doc:`packages<packages>` for :ref:`document

Reference: `typing – Support for type hints <https://docs.python.org/3/library/typing.html>`__

Exception handling
------------------
Exceptions
----------

- Do not raise `built-in exceptions <https://docs.python.org/3/library/exceptions.html>`__. Define specific exceptions in an ``exceptions.py`` module. For example:

.. code-block:: python
class ProjectNameError(Exception):
"""Base class for exceptions from within this package/application"""
class SpecificNameError(ProjectNameError):
"""Raised if this clear condition is true"""
- Do not use a bare ``except:`` or a generic ``except Exception:``. Use specific error classes to avoid handling exceptions incorrectly.
- Do not catch an exception and raise a new exception, *unless* the new exception has a special meaning (e.g. ``CommandError`` in Django).
Expand Down

0 comments on commit e309ed4

Please sign in to comment.