exception handling for a function returning several values

Discussion in 'Python' started by beliavsky@aol.com, Feb 12, 2005.

  1. Guest

    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.
     
    , Feb 12, 2005
    #1
    1. Advertising

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

    Steve
     
    Steven Bethard, Feb 12, 2005
    #2
    1. Advertising

  3. Erik Johnson Guest

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

    HTH! :)
    -ej
     
    Erik Johnson, Feb 12, 2005
    #3
  4. 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.


    what about the following.

    def foo(x):

    # code setting y and z
    return x,z

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

    --
    Antoon Pardon
     
    Antoon Pardon, Feb 12, 2005
    #4
  5. 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...

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Feb 12, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorgen Gustafsson
    Replies:
    4
    Views:
    591
    Jorgen Gustafsson
    Dec 12, 2003
  2. Peter
    Replies:
    34
    Views:
    1,975
    James Kanze
    Oct 17, 2009
  3. VSK
    Replies:
    0
    Views:
    255
  4. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    188
    Max Lapshin
    Mar 16, 2007
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    200
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page