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


Groups > comp.lang.python > #59135

Re: Where to handle try-except - close to the statement, or in outer loop?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.068
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'stops': 0.07; 'http': 0.09; 'separately': 0.09; 'try:': 0.09; 'blanket': 0.16; 'catch- all': 0.16; 'dog': 0.16; 'exception?': 0.16; 'feeding': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'independent,': 0.16; 'ioerror': 0.16; 'wrote:': 0.18; 'issue.': 0.22; 'putting': 0.22; 'errors.': 0.24; 'handling': 0.26; 'header :In-Reply-To:1': 0.27; 'point': 0.28; 'errors': 0.30; 'message- id:@mail.gmail.com': 0.30; 'catching': 0.31; 'exceptions': 0.31; 'sense': 0.34; 'subject:the': 0.34; 'problem': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'next': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'should': 0.36; 'wrong': 0.37; 'sometimes': 0.38; 'depends': 0.38; 'nov': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; '12,': 0.39; 'reported': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'catch': 0.60; 'most': 0.60; 'him,': 0.64; 'more': 0.64; '(any': 0.84; '(probably': 0.84; 'subject:Where': 0.84; 'subject:handle': 0.84; 'subject:try': 0.84; 'victor': 0.84; 'recover': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=CVKTm3DXjY1OLSAQZODw4bEeKMryfxDdRVJzjC3asBQ=; b=A2sdloTPbZQsU0ikhyzNNRRJTm1lC13mTkN+w3x2iB5oxlHiverNn8B3ruC8i/TC+K o9LPfB/XTXu5rBqOvKTLYqbKDwI+2JlSDfl+QtyMy43dE3ZqwyYjhSbLmoSuDTJ+GyIL CV3ShJFHh5SlrABMjVMUJHB4MgmLpRXo02GgTFhBPLs2uwzpFSbB6VWKyUB+al8UqdNv VGCDsHJnIQHqnlu9/cT0zrKgKrKhWedpsZ6YyXBEOTAYLWQOzUHK95RkxN6uEw1S4IFv tWLUCyoQZDESBTXGs+yAhdh8QVTN8czDq1FDUi9Vn7pU0Qk4s3iWOiU2N7bXW5VN+nUM 5xtA==
MIME-Version 1.0
X-Received by 10.66.138.40 with SMTP id qn8mr5859175pab.154.1384221403887; Mon, 11 Nov 2013 17:56:43 -0800 (PST)
In-Reply-To <83569235-e2b6-40f8-b164-76eca0e20681@googlegroups.com>
References <83569235-e2b6-40f8-b164-76eca0e20681@googlegroups.com>
Date Tue, 12 Nov 2013 12:56:43 +1100
Subject Re: Where to handle try-except - close to the statement, or in outer loop?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2418.1384221413.18130.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384221413 news.xs4all.nl 15892 [2001:888:2000:d::a6]:54938
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59135

Show key headers only | View raw


On Tue, Nov 12, 2013 at 12:34 PM, Victor Hooi <victorhooi@gmail.com> wrote:
> Would I wrap all of the calls in a try-except block?
>
>     try:
>         my_pet.feed()
>         my_pet.shower()
>     except IOError as e:
>         # Do something to handle exception?
>

It really depends more on how you go about recovering from errors. If
feeding the dog and showering the dog are completely independent, you
should catch errors for them separately (probably inside feed() and
shower()), but if a problem with feeding the dog stops you from
showering him, then do what you have here. Catch exceptions where it
makes sense to recover from the issue. Sometimes that means putting a
blanket catch-all at some point ("if anything goes wrong here, log the
error, return an HTTP 500, and go deal with the next query"), and
sometimes it means not catching errors at all (any that bubble all the
way up will get reported on STDERR, which is often the most useful
handling anyway).

ChrisA

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


Thread

Where to handle try-except - close to the statement, or in outer loop? Victor Hooi <victorhooi@gmail.com> - 2013-11-11 17:34 -0800
  Re: Where to handle try-except - close to the statement, or in outer loop? Chris Angelico <rosuav@gmail.com> - 2013-11-12 12:56 +1100
  Re: Where to handle try-except - close to the statement, or in outer loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-12 02:03 +0000
  Re: Where to handle try-except - close to the statement, or in outer loop? Terry Reedy <tjreedy@udel.edu> - 2013-11-11 21:51 -0500

csiph-web