try-except syntax

Ian Kelly ian.g.kelly at gmail.com
Thu Apr 5 17:34:41 EDT 2018


On Thu, Apr 5, 2018 at 3:04 PM, ElChino <elchino at cnn.cn> wrote:
> I'm trying to simplify a try-except construct. E.g. how come
> this:
>   try:
>     _x, pathname, _y = imp.find_module (mod, mod_path)
>     return ("%s" % pathname)
>   except ImportError:
>     pass
>   except RuntimeError:
>     pass
>     return ("<unknown>")
>
> Cannot be simplified into this:
>   try:
>     _x, pathname, _y = imp.find_module (mod, mod_path)
>     return ("%s" % pathname)
>   except ImportError:
>   except RuntimeError:
>     pass
>     return ("<unknown>")
>
> Like a "fall-through" in a C-switch statement.

You're looking for:

  try:
    _x, pathname, _y = imp.find_module (mod, mod_path)
    return ("%s" % pathname)
  except (ImportError, RuntimeError):
    return ("<unknown>")


More information about the Python-list mailing list