Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98275

Re: How to handle exceptions properly in a pythonic way?

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to handle exceptions properly in a pythonic way?
Date 2015-11-05 15:51 +1100
Message-ID <mailman.44.1446699105.16136.python-list@python.org> (permalink)
References <4b303213-62e2-42d4-b2f6-4fc1f6025944@googlegroups.com> <n18fmg$oj6$1@reader1.panix.com> <e134f1a3-89cc-4497-807e-3dd0836fb5e6@googlegroups.com> <mailman.43.1446695916.16136.python-list@python.org> <4cb51da8-9331-4efe-87d3-dfe9c2ae0f8e@googlegroups.com>

Show all headers | View raw


On Thu, Nov 5, 2015 at 3:18 PM,  <zljubisic@gmail.com> wrote:
>> Which would you prefer?
>
> So if I am just checking for the ConnectionError in get_html and a new exception arises, I will have traceback to the get_html function showing that unhandled exception has happened.
> Now I have to put additional exception block for managing the new exception in the get_html function and I am covered.
>
> Is that what you wanted to say?

Yep. I would definitely recommend having get_html raise an exception
on any problem; here's the structure I'd use:

def get_html(...):
    try:
        ... actually go get the info
        return info
    except (ConnectionError, OSError, SocketError) as e:
        raise ContentNotFoundError from e

This means there are three possibilities:

1) Everything works correctly, and get_html returns something useful.

2) An exception occurs of a type that you expect - the server didn't
respond, or the OS threw back a failure, or whatever. (You'd decide
what set of exceptions this represents.) In this case, you get a
single exception that wraps that up and summarizes the problem as
"Content Not Found". The original exception is retained in the
__cause__ of the ContentNotFoundError, so no information is lost.

3) An unexpected exception occurs - your script runs out of memory, or
there's a bug in your code, or anything else. The exception will
simply bubble up, and be dealt with somewhere else.

To use this code, what you'd do is:

try:
    data = get_html(...)
except ContentNotFoundError:
    # deal with the case of not having anything to work with

Omitting the try/except is a perfectly reasonable way of working, too.
Just call get_html, and if ever it can't get a result, the exception
continues to propagate. It's easy, it's straight-forward, and it's
unambiguous - any problem you don't expect becomes an exception.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to handle exceptions properly in a pythonic way? zljubisic@gmail.com - 2015-11-02 11:24 -0800
  Re: How to handle exceptions properly in a pythonic way? John Gordon <gordon@panix.com> - 2015-11-02 20:05 +0000
    Re: How to handle exceptions properly in a pythonic way? zljubisic@gmail.com - 2015-11-04 19:41 -0800
      Re: How to handle exceptions properly in a pythonic way? Chris Angelico <rosuav@gmail.com> - 2015-11-05 14:58 +1100
        Re: How to handle exceptions properly in a pythonic way? zljubisic@gmail.com - 2015-11-04 20:18 -0800
          Re: How to handle exceptions properly in a pythonic way? Chris Angelico <rosuav@gmail.com> - 2015-11-05 15:51 +1100
            Re: How to handle exceptions properly in a pythonic way? zljubisic@gmail.com - 2015-11-09 13:43 -0800
          Re: How to handle exceptions properly in a pythonic way? tian.su.yale@gmail.com - 2015-11-04 21:02 -0800
  Re: How to handle exceptions properly in a pythonic way? Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-02 13:58 -0700
    Re: How to handle exceptions properly in a pythonic way? zljubisic@gmail.com - 2015-11-04 20:11 -0800
  Re: How to handle exceptions properly in a pythonic way? tian.su.yale@gmail.com - 2015-11-04 21:15 -0800
    Re: How to handle exceptions properly in a pythonic way? zljubisic@gmail.com - 2015-11-09 13:36 -0800

csiph-web