purpose:
Make your code robust and protect your code from invalid data, events or other unexpectable impact.
good habits:
-- Handle for invalid inputs (data from external sources, routine input parameters)
a. garbage in, nothing out
b. garbage in, error message out
c. no garbage allowed in
-- Build your own assert machanism (for debugging purpose)
a. avoid putting executable code into assertion (otherwise will miss some execution code when releasing)
-- Error-Handling Techniques
a. return a neutral/closest value
b. skip to next valid data
c. return last valid data
d. Log a warning message to a file (Log file)
e. Return a defined error code (Throw an exception)
f. call an error-processing routine/object
g. display an error message (System.out.print)
h. shut down
-- tips for Exception mechanism
a. avoid throwing exceptions in constructors and destrictors unless you catch them in the same place.
b. throw exceptions at the right level of abstraction
c. avoid empty catch blocks
d. consider creating your own project-specific
exception class, which can serve as the base class for all exceptions
thrown on your project. This supports centralizing and standardizing
logging, error reporting and so on.
-- Damage-containment strategy: Barricade your program (add a firewall
layer for your code which maybe contains Assertions and Error-handlers)
-- Debugging Aids
a. use offensive programming to notify yourself the potential errors
b. handle exception cases in a way that makes them
obvious during development and recoverable when production code is
running.
c. use version-control tools and build tools like ant and make