Master java skills

Java Exception Types

Exceptions can be divided into two major categories.

Error

Error is a subclass of Throwable. Errors are those situations which tells that there is some severe problem which is causing the abrupt ending of the program. Errors although can be caught using Exception handling mechanism, but should not be handled programatically.

Below are some of the examples of Error classes

  1. AbstractMethodError
  2. OutOfMemoryError
  3. IllegalAccessError
  4. ClassFormatError
  5. NoClassDefFoundError
  6. NoSuchFieldError
  7. StackOverFlowError
  8. ThreadDeath
  9. UnsupportedClassVersionError
  10. VirutalMachineError

Exception

Exception class is the child class of Throwable. Exceptions can further be divided into two major categories : Checked Exceptions and Unchecked Exceptions. Checked Exceptions are also known as Compile-time Exceptions; whereas Unchecked Exceptions are also known as Runtime Exceptions.

Checked Exceptions

Checked Exceptions are extended from Exception class. These are also known as Compile-Time Exceptions because complier checks at the time of compilation whether such exceptions are handled or not. Developers must handle them. If not, then compiler will give error. Examples of such exceptions are : IOException, FileNotFoundException, ClassNotFoundException, NoSuchMethodException, SQLException etc.

Unchecked Exceptions

Unchecked Exceptions are extended from RunTimeException class. All the subclasses of RunTimeException are unchecked exceptions. They are also known as runtime exceptions because compiler doesn’t bother about such exceptions. Whether they are handled or not, it not a matter of concern for the compiler. But if they are not handled, and they occur at runtime, it will still cause the program to deviate from a normal execution and it would terminate as well.

Examples of such exceptions are : ArithmeticException, NullPointerException, NumberFormatException, IndexOutOfBoundsExceptions etc.

Difference between Errors and Exceptions

ExceptionError
Should be handledShould not be handled
Can be checked as well as uncheckedErrors are always unchecked
If handled correctly, program can recover from an exception scenarioProgram cannot recover from an Error scenario.
Program logic is responsible for causing exception in most of the scenariosEnvironment in which the program is running is responsible for Error to occur.
java.lang.NullPointerException, java.lang.ArithmeticException are some of the examples
java.lang.StackOverflowError, java.lang.OutOfMemoryError are some of the examples