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


Groups > comp.lang.python > #98114

How to handle exceptions properly in a pythonic way?

Newsgroups comp.lang.python
Date 2015-11-02 11:24 -0800
Message-ID <4b303213-62e2-42d4-b2f6-4fc1f6025944@googlegroups.com> (permalink)
Subject How to handle exceptions properly in a pythonic way?
From zljubisic@gmail.com

Show all headers | View raw


Let's say that I have the following simple function:

def get_html(url):

    wpage = requests.get(url)
    
    return wpage.text

How to handle exceptions properly that can arise during execution of the requests.get(url)?

If I call this function with

try:
    html = get_html('www.abc.com/index.html')
except ConnectionError:
        service_exception
else:
    continue_with_the_code

and ConnectionError exception happens I can handle the exception with service_exception part of the code.

I could also put some try/except block in get_html function, but the only purpose why should I do it,  is to log the url and re rise the error. Is there any other reason why should I do this?
 
def get_html(url):

    try:
        wpage = requests.get(url)
    except ConnectionError:
        flog('warning', 'ConnectionError exception', log_except=True)
        raise

    return 

Anyway, whenever I am calling the get_html function I should put it in the try/except block and handle all exceptions.
Now which exceptions? Can I know them in advance or I should wait for one to happened and then put it in caller except block?

Let's say that I have called get_html function hundred times in try/except block with ConnectionError part, and now I found a new exception that can happen during execution of the requests.get(url) command. What should I do now?
Should I put the same except block that handles that new exception in every and each of these hundred calls? 

I could also handle all exceptions in get_html function, and in case that I am not able to get the html, I can return None. 

def get_html(url):

    try:
        wpage = requests.get(url)
    except ConnectionError:
        flog('warning', 'ConnectionError exception', log_except=True)
        raise
    except Exception as e:
        flog("warning", "{0}   [wpage = requests.get(url) {1}]".format(e, url), log_except=True)
        raise
    else:

        if wpage.status_code == requests.codes.ok:
            flog('debug', 'wpage.status_code = %s' % wpage.status_code)
            html = wpage.text
        else:
            flog('debug', 'url = %s' % url)
            html = None

    return html


Now caller should only check if function has returned something other than None. Caller doesn't need try/except block anymore. Something like this:

if get_html('www.abc.com/index.html') is not None:
    I_am_sure_that_html_has_been_downloaded_correctly


Now if I want to catch a new exception, I can catch it in get_html function, which is the only change in the program.

I have read some articles that returning None is not a good approach, so I am confused.

How to handle exceptions properly in a pythonic way?

Regards.

Back to comp.lang.python | Previous | NextNext 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