3 ms·
>try: >> f = float(someText) >> catch ValueError: >> # I just parsed you, this is crazy, >> # here's an exception, throw it maybe? No, you ha
by flyinRyan 14y ago
>try:
>> f = float(someText)
>> catch ValueError:
>> # I just parsed you, this is crazy,
>> # here's an exception, throw it maybe?
No, you have it exactly backwards. It's with return values that you have to check after every call to be safe. With exceptions you can truly say: if I don't know what to do if this fails then I don't do anything. You can let the exception bubble up higher, all the way up to crashing the program if you like (which gives a nice stack trace that can then be debugged).
With return values, if you don't check then you could be building up more and more trash and you won't even know it until you either hit a point of code that finally does look at what's returned, or if an exception occurs (e.g. segfault).
- Edootjuh 14y agotry: f = float(someText) catch ValueError: # I just parsed you, this is crazy, # here's an exception, throw it maybe? Formatted That For You
- deleted 14y ago[deleted]
- Evbn 14y ago"Not checking" is just shorthand for pythons " except: pass".
- prodigal_erik 14y agoBut not checking is invisible. I can't grep for it. It doesn't stand out in a code review. It's almost never what I meant to do, so it shouldn't have been the default or easily overlooked, much less both.