Exceptions

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:

Error

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
(JVM)

Error

AssertionError1

ExceptionInInitializerError2

NoClassDefFoundError3

StackOverflowError

Checked Exceptions
(Application)

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…)

(1) When the assertion boolean expression returns false
(2) When any exception is thrown from a static initializer (variable or block)
(3) Class not found in the classpath at runtime
(4) E.g. division by zero
(5) Application exceptions (not JVM)
(6) JVM access restrictions

That’s all coders! If you have any comments or questions, you can drop me a line or leave a comment below and I will be pleased to help you.

In case you’re looking for a complete (but still short, precise and to-the-point) study guide to help you get your valuable Oracle® Java Programmer certification, including many tricky real-exam-like questions, study sheets, and other bonuses, then the Java Certification Roadmap book is your book.

Finally, if you found this content useful and know more programmers, colleagues or friends that may be interested in getting the Java Certification, click one of the buttons belowclick the button on the bottom left corner of your screen to let them know about the Java Certification Roadmap.