Sunday, February 10, 2013

Java Exceptions

The Throwable class is the superclass of all errors and exceptions in the Java language.
Then we have Error and Exception classes, that implement the Throwable interface, and there can be checked or unchecked exceptions:
  • Checked exceptions are the exceptions that need to be catch in a try catch block or need to be declared in the method signature where they are thrown.
  • Unchecked exceptions are RuntimeExceptions (or Errors) that do not need to be caught or declared in the method signature.


RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeExceptions that are currently part of the certification objectives are:
ArrayIndexOutOfBoundsException - when attempting to access an array with an invalid index value
ClassCastException - when attempting to cast a reference variable to a type that fails the IS-A test
IllegalArgumentException - when the method receives an argument formatted differently that the method expects
IllegalStateException - when the state of the environment doesn't match the operation being attempted
NullPointerException - when attempting to access an object with a null reference variable
NumberFormatException - when a method that converts a String to a number, receives a String that it cannot convert.

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Errors that are currently part of the certification objectives are:
AssertionError - if the assert boolean test evaluates to false
ExceptionInInitializerError - error while attempting to initialize a static variable ou an initialization block
StackOverflowError - when a method recurses too deeply, and exhausts the stack
NoClassDefFoundError - when the JVM can't find the class that it needs (command line error, classpath issue ou missing .class file)

No comments:

Post a Comment