Exeptions and Errors in Java

In the diagram below I present the hierarchy of most common Exceptions and Errors:


Object
 |
 |_ Throwable
    |
    |_ Error
    |   |
    |   |_ AssertionError
    |   |
    |   |_ LinkageError
    |   |   |
    |   |   |_ ExceptionInInitializerError
    |   |   |
    |   |   |_ NoClassDefFoundError
    |   |   |
    |   |_ VirtualMachineError
    |       |
    |       |_ StackOverflowError
    |
    |_ Exception
        |
        |_ RuntimeException
            |
            |_ NullPointerException
            |
            |_ IllegalArgumentException
            |   |
            |   |_ NumberFormatException
            |
            |_ IndexOutOfBoundException
            |   |
            |   |_ ArrayIndexOutOfBoundException
            |
            |_ ClassCastException
            |
            |_ IllegalStateException

For SCJP purposes, one needs to recognize which of them are thrown by the virtual machine (JVM) and which should be thrown programmatically. Read below, to learn that (those Errors and Exceptions which are not so common are briefly described):

Thrown by JVM

  • NullPointerException
  • StackOverflowError – when stack overflow occurs because of too deep recursion
  • ArrayIndexOutOfBound – when a negative index or index that is greater or equal to the size of an array was used to get an element of that array
  • ClassCastException
  • ExceptionInInitializerError – raised when an unexpected exception occured in a static initializer, i.e. exception occured during evaluation of a static initializer or the initializer for a static variable
  • NoClassDefFoundError – when JVM or ClassLoader instance tried to load in the definition of a class, and that definition wasn’t found

Thrown programatically

  • IllegalArgumentException – when a method was passed an illegal or inappropriate argument
  • NumberFormatException – when convertion of String to one of numeric type failed
  • IllegalStateException – when a method was invoked at an illegal or inappropriate time (e.g. using Scanner that has been closed)
  • AssertionError – when assertion failed
Previous Post
Next Post