Introducing Exceptions

An exception is an event that disrupts the normal flow of execution in a program. Some of the advantages of using exceptions are:

  • Separating error-handling code from regular code
  • Easily differentiating error types
  • Being able to propagate errors up the call stack

For the exam, you should be familiar with the following concepts:

Call Stack  List of methods that had been called to get to the method where the error occurred
Exception Handler  A block of code that can handle the exception. If the runtime system doesn’t find an appropriate exception handler in the call stack to catch the exception, it terminates the thread throwing the exception

Kind of Exceptions

There are two groups of exceptions:

 Checked Exceptions
  • All exceptions except Error, RuntimeException and their subclasses
  • Subject to the Catch or Specify Requirement: code that might throw these exceptions must be enclosed by try or throws
  • They are used to notify exceptional situations in the program
 Unchecked Exceptions
  • Error, RuntimeException and their subclasses
  • Don’t need to be declared in a method or constructor’s throws clause
  • They are exceptional conditions that the application usually cannot anticipate or recover from (though it could try)

As it’s shown in the previous table, an unchecked exception can be either an Error or a RuntimeException:

  • External to the application, related to the environment (memory problem, class not found, etc)
  • Thrown only by the JVM. An application should not try to catch
Runtime Exception
  • Internal to the application (e.g. a programming bug can cause a NullPointerException)
  • Notify exceptional situations that cannot be predetermined at compile time
  • Generally are only thrown by the JVM, but an application can also throw them if it makes sense in a given situation

The Throwable Family

It could seem that the table below just shows some examples of Java exception classes, but it doesn’t. It actually shows the minimum exception class hierarchy you should be familiar to: Throwable (the base class, the father of all exceptions), its two sons (Error and Exception) and its main grandchildren. Trust me when I tell you that you should learn the content of this table before trying to do the exam.

Unchecked Exceptions
  • Error
  •    AssertionError1      
  •    ExceptionInInitializerError2
  •    NoClassDefFoundError3
  •    StackOverflowError
Checked Exceptions
  • Exception
  •    CloneNotSupportedException
  •    IOException
  •    FileNotFoundException
  • (… all other checked exceptions…)
Unchecked exceptions
(JVM or Application)
  •    RuntimeException
  •         ArithmeticException4
  •         ClassCastException
  •         IllegalArgumentException5
  •             NumberFormatException5
  •         IllegalStateException5
  •         IndexOutOfBoundsException
  •             ArrayIndexOutOfBoundException
  •             StringIndexOutOfBoundException
  •             NullPointerException
  •         SecurityException6
  •    (… all other runtime exceptions…)

To learn more about exceptions and how to deal with them read the lesson about catching and handling exceptions.

Leave a Reply