Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'situation.': 0.05; 'received:verizon.net': 0.07; 'terry': 0.07; 'exception.': 0.09; 'exceptions': 0.09; "object's": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; 'exception': 0.12; 'called,': 0.16; 'close()': 0.16; 'constructor.': 0.16; 'exceptions.': 0.16; 'failure.': 0.16; 're- raise': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:possible': 0.16; 'wrote:': 0.18; 'instance': 0.18; 'jan': 0.19; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'ignore': 0.26; 'function': 0.27; 'raise': 0.28; 'looks': 0.29; 'second': 0.29; 'outer': 0.30; 'parent': 0.30; 'least': 0.30; 'list': 0.32; 'objects': 0.32; 'header:User-Agent:1': 0.33; 'header:X -Complaints-To:1': 0.33; 'object': 0.33; 'to:addr:python-list': 0.34; 'it.': 0.34; 'possible,': 0.34; 'all.': 0.34; 'preserve': 0.34; 'subject:/': 0.35; '...': 0.36; 'two': 0.37; 'but': 0.37; 'could': 0.37; 'received:org': 0.38; 'some': 0.38; 'fail': 0.39; 'files': 0.39; 'to:addr:python.org': 0.40; 'custom': 0.61; 'further': 0.64; 'strategy': 0.64; 'here': 0.65; 'imagine': 0.71; 'subject:face': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Close as Many Files/External resourcs as possible in the face of exceptions Date: Mon, 21 Nov 2011 16:48:37 -0500 References: <2e8d4372-393d-4c55-aa6e-6c9afd6c8ef2@n35g2000yqf.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321912144 news.xs4all.nl 6916 [2001:888:2000:d::a6]:42056 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16051 On 11/21/2011 7:09 AM, Mel Wilson wrote: > GZ wrote: >> Here is my situation. A parent object owns a list of files (or other >> objects with a close() method). The close() method can sometimes fail >> and raise an exception. When the parent object's close() method is >> called, it needs to close down as many files it owns as possible, even >> if the close() function of some files fail. I also want to re-raise at >> least one of the original exceptions so that the outer program can >> handle it. > [ ... ] >> >> It will re-raise the first exception and preserve the context and >> close as many other files as possible while ignoring any further >> exceptions. >> >> But this looks really awkward. And in the case that two files fail to >> close, I am not sure the best strategy is to ignore the second failure. > > I imagine you could save any caught exception instances in a list and > study them later. Yes, I would raise a custom exception instance that takes such a list of failures in its constructor. Give it a custom __str__ method to display them all. -- Terry Jan Reedy