Python: how to avoid code duplication in exception catching?

我怕爱的太早我们不能终老 提交于 2019-12-10 12:47:18

问题


What is a good pattern to avoid code duplication when dealing with different exception types in Python, eg. I want to treat URLError and HTTPError simlar but not quite:

try:
    page = urlopen(request)
except URLError, err:
    logger.error("An error ocurred %s", err)
except HTTPError, err:
    logger.error("An error occured %s", err)
    logger.error("Error message: %s", err.read())

In this example, I would like to avoid the duplication of the first logger.error call. Given URLError is the parent of HTTPError one could do something like this:

except URLError, err:
    logger.error("An error occurred %s", err)
    try:
         raise err
    except HTTPError, err:
         # specialization for http errors
         logger.error("Error message: %s", err.read())
    except:
        pass

Another approach would be to use isinstance eg. if URLError and HTTPError would not be in a chain of inheritance:

except (URLError, HTTPError), err:
    logger.error("An error occured %s", err)
    if isinstance(err, HTTPError):
         logger.error("Error message: %s", err.read())

Which one should I prefer, is there another better approach?


回答1:


I think that your third example is the best solution.

  • It's the shortest version
  • It avoids duplication
  • It is clear to read and easy to follow, much unlike the second version.

You might want to use the newer except FooError as err syntax, though, if you're on Python 2.6 or higher.

Also, in your example, the first version isn't quite correct since the URLError handler already catches the HTTPError, so the except HTTPError part is never reached. You'd have to switch the two excepts around. Another reason not to use this.



来源:https://stackoverflow.com/questions/7712651/python-how-to-avoid-code-duplication-in-exception-catching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!