Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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.
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
main()
    ...
    result = do_something(filename)
    ...
    ...
    do_something_else(result)
  • This can happen even if we don't pass the result around:
main()
    ...
    do_something(filename)
    ...
    ...
    do_something_else_assuming_the_other_worked()