Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Exceptions

Name: newfag 2012-04-22 10:59

What is the best way of dealing with invalid argument?
Throw an exception or return null?

Lets have an example. I was writing this function that
creates date objects for dates in between 2000-01-01 and
2999-12-31

Looks like this:
<code>
def createDate(date):
    try:
        y, m, d = date
        if(y < 0): return None
        if(y < 2000): y += 2000
        if(y > 2999): return None
        return datetime.date(y, m, d)
    except ValueError:
        return None
</code>

What should i do here? Should i just let the ValueError
error propagate up the stack, maybe throw my own ValueError exceptions in stead of returning None or should i
invent my own exception and throw that instead of ValueError,
or just leave it as it is?


What would you do?

>in b4 "dat shitty language"

Name: Anonymous 2012-04-24 6:08

>>6
Pattern shmattern.  Python is already quite slow, so having tons of exceptions does not subtract much from its turtle speed, however it adds some clarity to your shitty Python code so that your retarded colleagues will hate you less.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List