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


Groups > comp.lang.python > #44653

Re: question about try/except blocks

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jeanpierreda@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'nested': 0.07; 'think,': 0.07; 'try:': 0.09; 'subject:question': 0.10; 'blocks': 0.16; 'ioerror': 0.16; 'open()': 0.16; 'oserror': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'work,': 0.20; 'error': 0.23; "shouldn't": 0.24; 'handling': 0.26; 'this:': 0.26; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'too.': 0.31; 'exceptions': 0.31; 'class': 0.32; 'stuff': 0.32; 'there,': 0.34; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'leads': 0.36; 'done': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'catch': 0.60; 'more': 0.64; 'itself?': 0.84; 'subject:try': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=Xf796BX35PU6nj9wOUqyPWNqJPJ3uLwmzsFq9bgRA4g=; b=SW+JerEw6OO7kqrwbNvNjuFEy1mzrRpSA/W376WK5a9y8589QINYFOqqpf7SnqORYh YA695ktrynpRvDo6dripZ07BGBNRL17DpJ+N/WZte/DuK2K/M0NdrOS2h9zTb7R3IQNS v9crRFnHjrZbjNsMkGz5NZmM4TCnaq8kvky/Dpu4btsdOrDOu0vp6k19KAesshpnc2BJ Nqr6FWV2ETfZ7nIjoXxsP0t8d/fntuzMdyAFUhdA9Drg10N5poOKLhG6+/FPkpT4RqOm MenMZHfjH3eUtgKNS+zKKUJVxoRKy0+6XWwuQv/zORr3HHesiIzCs4G11BSzKXVAeWHA ID7A==
X-Received by 10.66.121.36 with SMTP id lh4mr12254732pab.208.1367547831720; Thu, 02 May 2013 19:23:51 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CAFB6qZsG6nns=_Vn5Fk6M8fvCVY_Ykh8ttH+r9Euz1+ed+iCRA@mail.gmail.com>
References <CAFB6qZsG6nns=_Vn5Fk6M8fvCVY_Ykh8ttH+r9Euz1+ed+iCRA@mail.gmail.com>
From Devin Jeanpierre <jeanpierreda@gmail.com>
Date Thu, 2 May 2013 22:23:11 -0400
Subject Re: question about try/except blocks
To Python List <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1261.1367547835.3114.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1367547835 news.xs4all.nl 15911 [2001:888:2000:d::a6]:32841
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44653

Show key headers only | View raw


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?

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


Thread

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

csiph-web