error handling in Python

Discussion in 'Python' started by beliavsky@aol.com, May 5, 2004.

  1. Guest

    Suppose I have a function

    def read_data(xfile):
    # code here to read dates and prices
    return dates,prices

    that if successful returns two Numeric arrays, of dates and prices. I
    am unsure what to return if read_data is unsucessful. Since a
    successful function call returns a list of length two, I could return
    a list of length one if there is a problem, so that [-1] is returned
    if the file does not exist, [-2] if the dates are invalid, etc. After
    calling the function, I could check the len of the result to detect
    problems. This seems workable but ad-hoc.

    I do NOT want to use the try/except idiom to stop the program if there
    is a problem reading data.

    In Fortran I would write a subroutine with calling sequence

    call read_data(xfile,dates,prices,ierr)

    where ierr would be checked upon return for nonzero values.
     
    , May 5, 2004
    #1
    1. Advertising

  2. wrote:

    > that if successful returns two Numeric arrays, of dates and prices. I
    > am unsure what to return if read_data is unsucessful. Since a
    > successful function call returns a list of length two, I could return
    > a list of length one if there is a problem, so that [-1] is returned
    > if the file does not exist, [-2] if the dates are invalid, etc. After
    > calling the function, I could check the len of the result to detect
    > problems. This seems workable but ad-hoc.


    are you aware of the multiple assignment capabilities of python? You can do
    it like this:

    def foo():
    a = compute_me()
    b = me_too()
    return a,b

    s, t = foo()

    so basically you can simply return always three values: your results, and an
    error value. The value then is checked.

    > I do NOT want to use the try/except idiom to stop the program if there
    > is a problem reading data.


    Why not? exceptions are definetely the best way (at least in python) to deal
    with error conditions. You should utilize them. And they don't stop the
    program - only if you want them to. The big advatage is that the programmer
    becomes aware of an error condition, while with your approach she can
    easily forget to check for the error.


    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, May 5, 2004
    #2
    1. Advertising

  3. John Roth Guest

    <> wrote in message
    news:...
    > Suppose I have a function
    >
    > def read_data(xfile):
    > # code here to read dates and prices
    > return dates,prices
    >
    > that if successful returns two Numeric arrays, of dates and prices.


    It looks like you're still thinking in Fortran. It's probably going to
    be easier all around to return one list of two element tuples.

    > I am unsure what to return if read_data is unsucessful. Since a
    > successful function call returns a list of length two, I could return
    > a list of length one if there is a problem, so that [-1] is returned
    > if the file does not exist, [-2] if the dates are invalid, etc. After
    > calling the function, I could check the len of the result to detect
    > problems. This seems workable but ad-hoc.




    > I do NOT want to use the try/except idiom to stop the program if there
    > is a problem reading data.
    >
    > In Fortran I would write a subroutine with calling sequence
    >
    > call read_data(xfile,dates,prices,ierr)
    >
    > where ierr would be checked upon return for nonzero values.


    The basic question is: what do you want to do with an error?
    That conditions how you want to handle it. Do you want to print
    an error message and quit? Print an error message and continue
    with the next batch? Fake up a return so the rest of the program
    continues as if nothing was wrong?

    John Roth
     
    John Roth, May 5, 2004
    #3
  4. Paul McGuire Guest

    <> wrote in message
    news:...
    >
    > I do NOT want to use the try/except idiom to stop the program if there
    > is a problem reading data.
    >

    Unlike FORTRAN, exceptions in Python do not necessarily stop the program -
    they only do so if not handled.

    In your case, you know just where read_data() is called from, so you can
    catch a thrown exception and do the appropriate
    cleanup/retry/shutdown/ignore-and-continue, all without having to figure out
    what "special" value to put in the return code to indicate a failure. You
    also don't have to clutter up your read_data() argument list with error
    codes, error descriptions, I/O status, etc. - put that stuff in attributes
    of the thrown exception, and leave your read_data() arguments to deal with
    the read_ing of data.

    Don't try to hack around try-except's mechanisms for invoking exception
    code, by using magic return codes ("if zero, that means success; if greater
    than zero, that's a warning; less than zero is an error") and exception-only
    method arguments. Cleaning this stuff out of your method signature is a
    *good* thing, and let's you focus on the functional task, while try-except
    offers a clear, robust, supported mechanism for signalling, um, let's not
    call them "failures", how about "alternative return paths"? :)

    -- Paul
     
    Paul McGuire, May 6, 2004
    #4
    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. Wolfram
    Replies:
    3
    Views:
    714
    Wolfram
    Dec 2, 2006
  2. Replies:
    1
    Views:
    344
    Diez B. Roggisch
    May 28, 2008
  3. Mark Tarver
    Replies:
    22
    Views:
    1,325
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,949
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    192
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page