Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to return a value AND raise an exception, does this mean I'm doing something wrong?

I have a number of functions that parse data from files, usually returning a list of results.

If I encounter a dodgy line in the file, I want to soldier on and process the valid lines, and return them. But I also want to report the error to the calling function. The reason I want to report it is so that the calling function can notify the user that the file needs looking at. I don't want to start doing GUI things in the parse function, as that seems to be a big violation of separation of concerns. The parse function does not have access to the console I'm writing error messages to anyway.

This leaves me wanting to return the successful data, but also raise an exception because of the error, which clearly I can't do.

Consider this code:

try:
    parseResult = parse(myFile)
except MyErrorClass, e:
    HandleErrorsSomehow(str(e))

def parse(file): #file is a list of lines from an actual file
    err = False
    result = []

    for lines in file:
        processedLine = Process(line)
        if not processedLine:
            err = True
        else
            result.append(processedLine)
    return result
    if err:
        raise MyErrorClass("Something went wrong")

Obviously the last three lines make no sense, but I can't figure out a nice way to do this. I guess I could do return (err, result), and call it like

parseErr, parseResult = parse(file)
if parseErr:
    HandleErrorsSomehow()

But returning error codes seems un-pythonic enough, let alone returning tuples of error codes and actual result values.

The fact that I feel like I want to do something so strange in an application that shouldn't really be terribly complicated, is making me think I'm probably doing something wrong. Is there a better solution to this problem? Or is there some way that I can use finally to return a value and raise an exception at the same time?

like image 804
Cam Jackson Avatar asked Sep 06 '11 00:09

Cam Jackson


4 Answers

Nobody says the only valid way to treat an "error" is to throw an exception.

In your design the caller wants two pieces of information: (1) the valid data, (2) whether an error occurred (and probably something about what went wrong where, so it can format a useful error message). That is a completely valid and above-ground case for returning a pair of values.

An alternative design would be to pass a mutable collection down to the function as a parameter and let it fill any error messages it wants to emit into that. That will often simplify the plumbing in the caller, especially if there are several layers of calls between the parser and the code that knows how to do something with the error messages afterwards.

like image 128
hmakholm left over Monica Avatar answered Nov 14 '22 22:11

hmakholm left over Monica


Emit a warning instead, and let the code decide how to handle it.

like image 45
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 00:11

Ignacio Vazquez-Abrams


Another possible design is to invert control, by passing the error handler in as a parameter.

(Also, don't feel like you have to tell Python how to accumulate data in a list. It knows already. It's not hard to make a list comprehension work here.)

def sample_handler():
    print "OMG, I wasn't expecting that; oh well."

parseResult = parse(myFile, sample_handler)

def parse(file, handler): #file is a list of lines from an actual file
    result = [Process(line) for line in data]
    if not all(result): handler() # i.e. if there are any false-ish values
    result = filter(None, result) # remove false-ish values if any
    return result
like image 23
Karl Knechtel Avatar answered Nov 14 '22 23:11

Karl Knechtel


Depending on caller design. using callbacks might be reasonable:

def got_line(line):
    print 'Got valid line', line

def got_error(error):
    print 'got error', error

def parse(file, line, error):
    for lines in file:
        processedLine = Process(line)
        if not processedLine:
            error(MyErrorClass("Something went wrong"))
        else
            line(processedLine)


parse(some_file, got_line, got_error)
like image 30
ymv Avatar answered Nov 15 '22 00:11

ymv