Handling errors as return values
- Each function that fails returns some error indicator. None ? An object that has and attribute "error"?
- None would be bad as that cannot indicate different errors.
- Every called needs to check if the function returned error. If at any point we forget our system might run with hidden failures.
examples/exceptions/demo1.py
def some_function() result = do_something(filename) if result: do_something_else(result) else: return result main() ... result = some_function()
- If we forget to check the result and pass it on, we might get some error in the code that is quite far from where the error actually happened
examples/exceptions/demo2.py
main() ... result = do_something(filename) ... ... do_something_else(result)
- This can happen even if we don't pass the result around:
examples/exceptions/demo3.py
main() ... do_something(filename) ... ... do_something_else_assuming_the_other_worked()