- Precondition: It's the caller's responsibility to pass good data
Use assert
to check and @pre
in the docstring for things that should never happen. Do not use them for error handling.
- Postcontition: What the function is guaranteed to do
- Class invariants: A condition that is true when function exits
Heisenbug: When the debugging changes the behavior of the system being debugged.
Exceptions should never be used for control flow. A well-designed API must not force its clients to use exceptions for ordinary control flow. Implement state-testing methods like hasNext()
to help client avoid try-catch.
Used for abnormal cases that can be recovered at runtime
- Client: catch or declare the exception
- API: declare in method signature
Recovery is impossible and continued execution would do more harm than good. Use runtime exceptions to indicate precondition violations (ex: ArrayIndexOutOfBoundsException
). All unchecked throwable should subclass RuntimeException
Turn a checked exception into an uncheched exception by using if-else instead of try-catch