Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: breaking out of outer loops Date: Mon, 7 Mar 2016 21:54:23 -0500 Lines: 30 Message-ID: References: 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: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104318 On 3/7/2016 6:49 PM, Chris Angelico wrote: > On Tue, Mar 8, 2016 at 10:42 AM, Chris Kaynor 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