[Tutor] How to return a list in an exception object?

Steven D'Aprano steve at pearwood.info
Thu Jun 18 00:00:31 CEST 2015


On Wed, Jun 17, 2015 at 12:49:38PM +0000, David Aldrich wrote:
> Hi
> 
> I have a function that compares a set of files with a reference set of 
> files.  All files are compared and then, if any differences were 
> found, an exception is raised:
[...]
> I would like to pass back to the caller a list of the files that 
> failed. How would I include that list in the exception object?

Others have already answered your question, but I'm completely with Alan 
on this one: don't do this. Have your function return a list of 
differences. If there are no differences, return an empty list. The 
caller then decides what to do:

diffs = checkfiles()
if diffs:
    for fname in diffs:
        print("file name %s is different" % fname)
        repair_file(fname)
else:
    raise NoDifferencesError(
        "expected some differences, but didn't find any"
        )


-- 
Steve


More information about the Tutor mailing list