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?
>>4
That's easy, all the errors are yours since you are the user of the functions.
Case solved, happy hacking.
Name:
Anonymous2012-04-22 14:25
exceptions are more time expensive, return null or an int
or a more pattern-oriented answer:
if you have a piece of code where failure is expected then it shouldn't be an exception, user input is not reliable input so it should be expected to be malformed, only use exception in really exceptional circunstances
Name:
Anonymous2012-04-22 14:37
>>6 if you have a piece of code where failure is expected then it shouldn't be an exception, user input is not reliable input so it should be expected to be malformed, only use exception in really exceptional circunstances
That's not what "exception" means (or should mean) in this case. Just because we're all expected to die someday doesn't mean that your own death isn't an exceptional circumstance for you.
As a rule, exceptions should be raised whenever the function could not do what it says on the tin. In practice, this means you have to think clearly about what to put on the tin. For instance, ''.toUpper() would not be changing any cases, but for a good definition of toUpper, that is not exceptional behavior.
Name:
Anonymous2012-04-22 17:35
>>7
By that logic you can take exceptions out altogether and just say if something goes wrong the return is NULL/0, exceptions should actually be used when a piece of code receives something it's description doesn't take into account; descriptions often do that
For instance you may want to optimize a piece of code deep in your program removing it's boundary checks, trusting it would be indeed exceptional if anything from withing your program made a malformed call to it, but it doesn't impossibilitate someone to years later pick your code apart and add something that wrecks your shit
tl;dr: you dying is not an exceptional event, you being reborn after being [i]freed[i] is
Name:
Anonymous2012-04-22 18:11
dying is not an exceptional event, you being reborn after being [i]freed[i] is
No, being reborn is not an exceptional event either, because there is no such thing. The only enlightenment that exists is realizing there is no enlightenment, it's all in your head, realizing you really are not intelligent, artistic or a beautiful unique snowflake, no one gives enough fucks about you to stalk you, no one will ever understand you, you will never understand anyone, you will never understand yourself, only you can cure your depression, you can never get "out of the box" because there is no fucking box, your ego will never be destroyed except when you die, you will not only eventually die but it will happen meaninglessly because that's how you define meaning, there is no such thing as true love or friendship, everything is a little bit shit, and to make something less shit something else has to be made more shit, but making something more shit does not mean making something else less shit, you will always want only what you cannot have, there is no "true identity", you are always part of the mainstream, other people will always influence your choices and thinking, you define all the patterns in the chaos, only you can dominate yourself, you define all concepts, you will always suck and be a loser because that's how you define sucking and being a loser, you will never quit 4chan, you will always be paranoid, you will always be obsessive, you will never travel in space/time, and most importantly, you are a silly boy. The holy night does not shine.
"You sure you want to be free? You've been free already." -Timothy Leary
now here's a little rap that goes 1-2-3
in which we lay down the law to this faggot op
do not try to catch what you cannot fix
and if something goes wrong just throw up some dicks
>>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.
>>1
The correct solution is to assert() or abort(). If an API is being misused, you should fail as quickly and as loudly as possible so that the programmer who fucked it up notices and can fix it (hopefully before he even commits his code.)
Unfortunately many languages don't let you do this, so throwing an exception is your best bet.
Name:
Coathanger Creig2012-04-25 4:16
Program defensively! Use exceptions only when you can't avoid using them!
Name:
Anonymous2012-04-25 8:07
>>What is the best way of dealing with invalid argument?
I think the best way, sometimes, is dealing with your faults in life, and listening to the people around you. Sometimes, you're just *that guy* that rages over some bitch turning you down, or some guy telling you you suck, and I bet you tell them off with logical-fallacies and all manners of delusional grandiose thinking. Well, you really need to just:
take a step back
breathe
and think about what you are going to do within the next 10 seconds of your life that won't send you to prison for being an overly-aggressive cunt.
It's ok to be 'wrong' so long as you learn from your mistakes