error handling in Python

beliavsky at aol.com beliavsky at aol.com
Wed May 5 11:41:22 EDT 2004


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.



More information about the Python-list mailing list