Java Exceptions  «Prev 

Java Checked and Unchecked Exceptions

Checked and Unchecked Exceptions

Question: What is the guideline for deciding between a checked and unchecked exception?
Answer: If a client can reasonably be expected to recover from an exception, make it a checked exception.

Exception Classifications

If a client cannot do anything to recover from the exception, make it an unchecked exception.
These categories are related to each other, and subclasses of the class java.lang.Exception are categorized as checked exceptions if they are not subclasses of the class java.lang.RuntimeException.
Subclasses of the class java.lang.RuntimeException are categorized as runtime exceptions. Subclasses of the class java.lang.Error are categorized as errors. Figure 2.8 illustrates the class hierarchy of these exception categories.

Figure 2.8: Object, Throwable, Error, Exception, RuntimeException

Java Complete Reference

Checked Exceptions

  1. ClassNotFoundException: Class not found.
  2. CloneNotSupportedException: Attempt to clone an object that does not implement the Cloneable interface.
  3. IllegalAccessException: Access to a class is denied. IllegalAccessException signals that a particular method could not be found.
  4. InstantiationException: Attempt to create an object of an abstract class or interface.
  5. InterruptedException: One thread has been interrupted by another thread.
  6. NoSuchFieldException: A requested field does not exist.
  7. NoSuchMethodException: A requested method does not exist.
  8. ReflectiveOperationException - Superclass of reflection -related exceptions(Added by JDK 7.)

Runtime - Unchecked Exceptions

  1. ArithmeticException: Arithmetic error, such as divide-by-zero.
  2. ArrayIndexOutOfBoundsException: Array index is out-of-bounds.
  3. ArrayStoreException: Assignment to an array element of an incompatible type.
  4. ClassCastException: Invalid cast.
  5. EnumConstantNotPresentException: An attempt is made to use an undefined enumeration value
  6. IllegalArgumentException: Illegal argument used to invoke a method.
  7. IllegalMonitorStateException: Illegal monitor operation, such as waiting on an unlocked thread.
  8. IllegalStateException: Environment or application is in incorrect state.
  9. IllegalThreadStateException: Requested operation not compatible with current thread state.
  10. IndexOutOfBoundsException: Some type of index is out-of-bounds.
  11. NegativeArraySizeException: Array created with a negative size.
  12. NullPointerException: Invalid use of a null reference.
  13. NumberFormatException: Invalid conversion of a string to a numeric format.
  14. SecurityException: Attempt to violate security.
  15. StringIndexOutOfBounds: Attempt to index outside the bounds of a string.
  16. TypeNotPresentException: Type not found. (Added by J2SE 5.)
  17. UnsupportedOperationException: An unsupported operation was encountered.