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.

Advertisements

wrote:
> If a function that normally returns N values raises an exception, what
> should it return?

Depends on what you want to do with the result of the function.
> N values of None seems reasonable to me, so I would
> write code such as
>
> def foo(x):
> try:
> # code setting y and z
> return y,z
> except:
> return None,None
>
> y,z = foo(x)

You almost never want a bare except. What exception are you catching?

My suspicion is that the code would be better written as:

def foo(x):
# code settying y and z
return y, z

try:
y, z = foo(x)
except FooError:
y, z = None, None

but I'm not sure if you really want y and z to be None if foo fails.
What do you do with y and z?

Advertisements

<> wrote in message
news:...
> 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.

On 2005-02-12, <> wrote:
> If a function that normally returns N values raises an exception, what
> should it return?

Maybe it shouldn't return anything but instead of cathing the
exception it should let the caller handle it.
> N values of None seems reasonable to me, so I would
> write code such as
>
> def foo(x):
> try:
> # code setting y and z
> return y,z
> except:
> return None,None
>
> y,z = foo(x)
>
> 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.

On 11 Feb 2005 17:01:56 -0800, declaimed the following
in comp.lang.python:
> If a function that normally returns N values raises an exception, what
> should it return? N values of None seems reasonable to me, so I would
> write code such as
>
A strict interpretation of the above, without your example code,
to me would indicate that there are no return values. IE "if a function
.... raises an exception" implies that the CALLER of the function has to
handle the exception.
> def foo(x):
> try:
> # code setting y and z
> return y,z
> except:
> return None,None
>
This code sample, however, says that an exception is raised
WITHIN the function, and also HANDLED within the function. The function
itself is NOT raising an exception. Unlike:

def foo(x):
try:
...
return y, z
except:
raise "FOO FAILURE, BAD 'x'" #I know, deprecated
> 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 is... Wrap the CALL to foo() in a try/except block, and have
foo() /raise/ an exception when it is unable to process. That way you
don't have to worry about testing a flag in the expected normal case,
and have a clear handler for the bad case IN THE CALLER...

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!