I have a function that can return one of three things:
- success (
True) - failure (
False) - error reading/parsing stream (
None)
My question is, if I'm not supposed to test against True or False, how should I see what the result is. Below is how I'm currently doing it:
result = simulate(open("myfile"))
if result == None:
print "error parsing stream"
elif result == True: # shouldn't do this
print "result pass"
else:
print "result fail"
is it really as simple as removing the == True part or should I add a tri-bool data-type. I do not want the simulate function to throw an exception as all I want the outer program to do with an error is log it and continue.
simulatefunction I catch all exceptions; I don't want anything that happens inside the simulator to stop the rest of the program running (and processing the next element). But the answers are making me change my mind.simulatehas things it can catch and retry, that's good. But if it "fails", it should not returnNone. It should just raise an exception to the script that called it. Either way,simulateis done. ReturningNoneisn't as helpful as raising a proper exception -- or allowing an exception to propagate throughsimulateinto the calling script for handling.except Exception:instead. This catches all "real" errors, along withWarningandStopIteration. It allowsKeyboardInterruptandSystemExitthrough though. If you really want to catch those, it's probably best to use another, outer try/except or some other structure that clearly documents your intent, since those are not "errors". (But I did say "almost never"... perhaps in your case you really do want to grab everything, and even prevent Ctrl-C orsys.exit()from exiting, etc.)