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


Groups > comp.lang.python > #16051

Re: Close as Many Files/External resourcs as possible in the face of exceptions

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Close as Many Files/External resourcs as possible in the face of exceptions
Date 2011-11-21 16:48 -0500
References <2e8d4372-393d-4c55-aa6e-6c9afd6c8ef2@n35g2000yqf.googlegroups.com> <jadf1g$hpp$1@speranza.aioe.org>
Newsgroups comp.lang.python
Message-ID <mailman.2936.1321912144.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

Close as Many Files/External resourcs as possible in the face of exceptions GZ <zyzhu2000@gmail.com> - 2011-11-20 21:46 -0800
  Re: Close as Many Files/External resourcs as possible in the face of exceptions Mel Wilson <mwilson@the-wire.com> - 2011-11-21 07:09 -0500
    Re: Close as Many Files/External resourcs as possible in the face of exceptions Terry Reedy <tjreedy@udel.edu> - 2011-11-21 16:48 -0500

csiph-web