exception handling for a function returning several values

B

beliavsky

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

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.
 
S

Steven Bethard

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?

Steve
 
E

Erik Johnson

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
 
A

Antoon Pardon

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.

what about the following.

def foo(x):

# code setting y and z
return x,z

try
y,z = foo(x)
except ... :
# Handle errors.
 
D

Dennis Lee Bieber

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...

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top