exception handling for a function returning several values

Erik Johnson spam at nospam.org
Fri Feb 11 21:17:43 EST 2005


<beliavsky at aol.com> wrote in message
news:3064b51d.0502111701.1731fef3 at posting.google.com...
> If I try to use y or z inappropriately when they are None, the program
> will stop. An alternative is to return an error flag in addition to y
> and z from function foo and check the value of the error flag in the
> calling program. This seems a bit awkward.

It seems to me you would be undermining the intent of exceptions here.

    If there is some reason foo() can't proceed with x, then either some
sort of a "standard exception" gets thrown as a result of trying to, or you
can detect the condition within foo() and raise a custom exception which
adequately decribes the problem. Smart callers of foo() are then coded with
the knowledge that not all values of 'x' are guaranteed to produce the
"normal" results, and have one or more 'except' clauses for them to do
whatever it is they think is most appropriate with that exception.

HTH! :)
-ej





More information about the Python-list mailing list