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


Groups > comp.lang.python > #44653 > unrolled thread

Re: question about try/except blocks

Started byDevin Jeanpierre <jeanpierreda@gmail.com>
First post2013-05-02 22:23 -0400
Last post2013-05-02 22:23 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: question about try/except blocks Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-05-02 22:23 -0400

#44653 — Re: question about try/except blocks

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2013-05-02 22:23 -0400
SubjectRe: question about try/except blocks
Message-ID<mailman.1261.1367547835.3114.python-list@python.org>
On Thu, May 2, 2013 at 9:54 PM, J <dreadpiratejeff@gmail.com> wrote:
> Would it be better to wrap the call and catch the OSError there, or
> wrap the whole with open() block in the function itself?
>
> My thought is to wrap the with open() call in the function so that I'm
> not wrapping the function call every time I use the class somewhere,
> but then I am not sure of that as it leads to nested try blocks like
> so:

It definitely shouldn't be done that way, since you might catch
exceptions in other circumstances too. Try this:

try:
    f = open(dest, 'wb', 0)
except OSError as exc:
    ...
with f:
    try:
        ...
    except IOError as exc:
        ...
    else:
        ...

-- Devin

> try:
>     with open(dest, 'wb', 0) as outfile:
>         try:
>             stuff
>         except IOError as exec:
>             more stuff
>         else:
>             other stuff
> except OSError as exc:
>      error handling stuff
>      return False
>
>
> I think, functionally, that should work, but should nested try/except
> blocks be avoided?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web