Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>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).



    try:
       f = float(someText)
    catch ValueError:
       # I just parsed you, this is crazy,
       # here's an exception, throw it maybe?
Formatted That For You


"Not checking" is just shorthand for pythons " except: pass".


But 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: