Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104318
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Terry Reedy <tjreedy@udel.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: breaking out of outer loops |
| Date | Mon, 7 Mar 2016 21:54:23 -0500 |
| Lines | 30 |
| Message-ID | <mailman.31.1457405705.15725.python-list@python.org> (permalink) |
| References | <nbl1md$1lvd$1@gioia.aioe.org> <mailman.1.1457392715.15725.python-list@python.org> <nbl2dd$1mkn$1@gioia.aioe.org> <CALvWhxuDbgE8AR+Q35hrqKhZ8Ft-n0CHUmFh8gYjtR6oG0nZ8g@mail.gmail.com> <CAPTjJmoghjqK+O_kU5a_oa8Fd9wG4eENGOQ7oYk0BADnwzW4OQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de Za4wydyg+mllPeaBkRygigpUlvLBGTHFAr9QrCKOzbNQ== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'line:': 0.07; 'exception,': 0.09; 'files:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'valueerror': 0.09; 'bug': 0.10; 'jan': 0.11; 'exception': 0.13; 'suggest': 0.15; '2016': 0.16; 'line.split()': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'rough': 0.16; 'subject:breaking': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'file:': 0.22; 'function:': 0.22; 'am,': 0.23; 'code,': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'error': 0.27; 'accidentally': 0.29; 'catching': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'class.': 0.30; 'strongly': 0.30; "i'd": 0.31; 'probably': 0.31; 'especially': 0.32; 'class': 0.33; 'errors,': 0.33; 'raising': 0.33; 'traceback': 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'possible': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'manual': 0.38; 'to:addr:python.org': 0.40; 'easy': 0.60; 'entire': 0.61; 'details': 0.62; 'received:96': 0.63; 'more': 0.63; 'due': 0.65; 'mar': 0.65; 'elsewhere': 0.66; 'fact,': 0.67; 'received:fios.verizon.net': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | pool-96-227-207-81.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 |
| In-Reply-To | <CAPTjJmoghjqK+O_kU5a_oa8Fd9wG4eENGOQ7oYk0BADnwzW4OQ@mail.gmail.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| 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> |
| Xref | csiph.com comp.lang.python:104318 |
Show key headers only | View raw
On 3/7/2016 6:49 PM, Chris Angelico wrote:
> On Tue, Mar 8, 2016 at 10:42 AM, Chris Kaynor <ckaynor@zindagigames.com> wrote:
>> And the same rough example, using an exception without a function:
>>
>> for file in files:
>> try:
>> for line in file:
>> section = line.split()
>> for section in line:
>> if sectionCorrupt:
>> raise Exception('Section corrupt') # You probably want
>> more details here, for possible manual repair. You could also have a custom
>> exception class for more control.
>> except Exception as e:
>> print('Failed to process the file {} with error {}.'.format(file,
>> str(e))) # Probably should also print the entire traceback to aid in
>> repairing errors, especially if they are due to a bug in the code, but this
>> is the rough idea.
>
> Yes, although I would more strongly suggest the custom exception
> class. In fact, the way I'd word it is: Never raise Exception, always
> a subclass. Otherwise it's too easy to accidentally catch something
> elsewhere in the code (a ValueError or TypeError or even
> AttributeError).
I would consider raising and catching StopIteration.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
breaking out of outer loops Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 18:09 -0500
Re: breaking out of outer loops Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-07 16:17 -0700
Re: breaking out of outer loops Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 18:21 -0500
Re: breaking out of outer loops Chris Kaynor <ckaynor@zindagigames.com> - 2016-03-07 15:42 -0800
Re: breaking out of outer loops Chris Angelico <rosuav@gmail.com> - 2016-03-08 10:49 +1100
Re: breaking out of outer loops Terry Reedy <tjreedy@udel.edu> - 2016-03-07 21:54 -0500
Re: breaking out of outer loops Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-03-07 23:29 +0000
Re: breaking out of outer loops Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 18:40 -0500
Re: breaking out of outer loops Chris Angelico <rosuav@gmail.com> - 2016-03-08 10:47 +1100
Re: breaking out of outer loops Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 19:02 -0500
Re: breaking out of outer loops Chris Angelico <rosuav@gmail.com> - 2016-03-08 11:08 +1100
Re: breaking out of outer loops Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 19:13 -0500
Re: breaking out of outer loops Joel Goldstick <joel.goldstick@gmail.com> - 2016-03-07 19:21 -0500
Re: breaking out of outer loops Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-08 00:36 +0000
Re: breaking out of outer loops Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-08 03:45 +0000
csiph-web