Graduate Program KB

Chapter 7 - Error Handling

  • Error handling obscures code logic often making it unreadable

  • Don't return error codes or error flags instead seperate your code and error handling

    • Use a try, catch
  • Use a try, catch, finally

    • You must not abruptly quit your code
    • A Try, Catch, Finally defines what the user of the code should expect no matter what goes wrong
  • Context

    • Provide informative error messages passed with your exception
    • Mention the operation and failure type
  • Checked Exceptions are Bad

    • Exceptions that run at compile time
  • Special Case Pattern

    • Get rid of error handling code
    • Instead use a class or configure an object so that it always returns something that will handle the special case for you
  • Don't return null from a method

    • Too many checks to handle (IF != NULL)
      • If you miss a check you get an error
  • Don't pass null to a method

    • When someone passes null as an argument we get a NullPointerException