The Most Diabolical Python Antipattern

Ian Kelly ian.g.kelly at gmail.com
Fri Jan 30 11:38:53 EST 2015


On Fri, Jan 30, 2015 at 8:56 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Ian Kelly <ian.g.kelly at gmail.com>:
>
>> Like I suggested earlier, just don't catch the inner exception at all.
>> The result will be both exceptions propagated, chained in the proper
>> order.
>
> Depends on the situation.

Like what? If you want to specifically propagate the original
exception in order to be caught again elsewhere, then I think there's
a code smell to that. If this inner exception handler doesn't
specifically know how to handle a ValueError, then why should some
outer exception handler be able to handle an exception that could have
come from virtually anywhere?

A better approach to that would be to create a specific exception
class that narrowly identifies what went wrong, and raise *that* with
the other exceptions chained to it. E.g.:

  try:
      do_interesting_stuff()
  except ValueError as e:
      try:
          log_it()
      except Exception:
          # Chain both exceptions as __context__
          raise SpecificException
      else:
          # Chain the original exception as __cause__
          raise SpecificException from e

Or if you don't care about distinguishing __cause__ from __context__:

  try:
      do_interesting_stuff()
  except ValueError:
      try:
          log_it()
      finally:
          raise SpecificException



More information about the Python-list mailing list