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