Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: breaking out of outer loops Date: Tue, 8 Mar 2016 10:49:19 +1100 Lines: 25 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de qxygxkXyRPy/XfD1My9oggWY3/ncY+hEMDomEE7S8C1w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'line:': 0.07; 'cc:addr :python-list': 0.09; 'exception,': 0.09; 'files:': 0.09; 'valueerror': 0.09; 'bug': 0.10; 'exception': 0.13; 'suggest': 0.15; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'line.split()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'rough': 0.16; 'subject:breaking': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'file:': 0.22; 'function:': 0.22; 'am,': 0.23; 'code,': 0.23; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'accidentally': 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; 'traceback': 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'skip:{ 10': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'manual': 0.38; 'received:209': 0.38; 'easy': 0.60; 'entire': 0.61; 'details': 0.62; 'more': 0.63; 'due': 0.65; 'mar': 0.65; 'elsewhere': 0.66; 'fact,': 0.67; 'chrisa': 0.84; 'to:none': 0.91 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:cc; bh=S/30kl+oQfQPdPg3fRNSHUgGZUg4726ddUG5TaR1UsQ=; b=ZhYvMPxfkYS9OfVzJNV2q3J2B9syRgnaY8otfRNnQ/RapISBnklthxS4p1nRFwiKtB Mq2xy2EJDbLXGJa9jUCIy1ins8dzSApTZEEj5ZWu9Lu2+pfuNgu3dgh1eggql3zbu5Uo +BYCzZ3dLRV14bvVBuKiCkJA3aNl1F/1tqXy+42vNxt4SH89P9lkbD9qhU1T+NiAtg5U DEls+LGvolUO9/yP1se8vEJSaHN6tTqsteuDGm55+ig4edkRa+7ZKC9C40QTvfxDtMsb bQan9hT5h77/z5/MQE6SeRk1BAcMWB5uGwKjwYT0Wp0pvJhjcYi2Bkxn+CKeYgPAZXPp 58Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=S/30kl+oQfQPdPg3fRNSHUgGZUg4726ddUG5TaR1UsQ=; b=Lm5rLTS68AEksevclOji6jHtGQnhYik9jGOx5TWSe89LOc9de9rbYs6yrTvQqOK1N2 Gxitifkxp2B4UOZyDXlsJ5rM+bXFxp5tVhvnSkDayp5pJxONKAv8L/kHRxIh735f3M2+ gh7RX92UBPlvxN5ZZxecgAqsiqu3ByzeYIVatYUiaq8ENzIDVID7Hmrr6qS2zQ8MIvVE AHTTPX3K7DqWz9ZYCU2NtjOqK/QkE23warcg6OG2dZd98wkIXYnpRFxwLMXL5NLAHZ/X Nd8MzHShqQDK2pgKwRx3r+dE5QbRP2fwJhq5zDcYUCQoMbB2EIFhstJlUMb/AoyTPNQM DrdQ== X-Gm-Message-State: AD7BkJLAw9Pa1SBSlHxziGeJKq3DgtZ4A7GnMamyx+hPBxIwZQ/rqe9ZBpgMKWBuflud10ExRivHnFMhokqrhA== X-Received: by 10.50.41.106 with SMTP id e10mr15771174igl.94.1457394559145; Mon, 07 Mar 2016 15:49:19 -0800 (PST) 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:104276 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). ChrisA