previous | start | next

Guidelines for using Java Exceptions

  1. Identify preconditions for the methods in classes you write.
  2. In each method check if its preconditions are met. If not, throw an appropriate exception.
  3. Include methods in your class that lets the user of the class test whether preconditions regarding the state of a class instance are met.
  4. Stick to the standard Java exception classes if possible. Usually you can find one that is appropriate for your purposes without having to create new Exception classes.

    A short list of commonly used java unchecked exceptions:

    1. IndexOutOfBoundsException (unchecked)
    2. IllegalArgumentException (unchecked)
    3. IllegalStateException (unchecked)
    4. NoSuchElementException (unchecked)
    5. UnsupportedOperationException (unchecked)


previous | start | next