OCA Java SE 8 Mock Exam Review - Java Exception Review

Exception handling

An exception is an object of the class java.lang.Throwable.

Enclose the code that may throw an exception within a try block.

Define catch blocks to include code to execute when an exceptional condition arises.

A try block can be followed by one or more catch blocks.

The catch blocks must be followed by zero or one finally block.

The finally block executes regardless of whether the code in the try block throws an exception.

A try block can't define multiple finally blocks.

The order in which the catch blocks are placed matters. If the caught exceptions have an inheritance relationship, the base class exceptions can't be caught before the derived class exceptions.

A finally block will execute even if a try or catch block defines a return statement.

A try block may be followed by either a catch or a finally block or both.

If both catch and finally blocks define return statements, the calling method will receive the value from the finally block.

If a catch block returns a primitive data type, a finally block can't modify the value being returned by it.

If a catch block returns an object, a finally block can modify the value being returned by it.

The finally block can't appear before a catch block.

You can rethrow an error that you catch in an exception handler.

You can either handle an exception or declare it to be thrown by your method.

You can create nested exception handlers.

A try, catch, or finally block can define another try-catch-finally block.

Categories of exceptions

Exceptions are divided into three categories: checked exceptions, runtime (or unchecked exceptions), and errors.

Subclasses of the class java.lang.RuntimeException are categorized as runtime exceptions.

Subclasses of the class java.lang.Error are categorized as errors.

Subclasses of the class java.lang.Exception are categorized as checked exceptions if they are not subclasses of class java.lang.Runtime.

The class java.lang.RuntimeException is a subclass of the class java.lang .Exception.

The class java.lang.Exception is a subclass of the class java.lang.Throwable.

The class java.lang.Error is also a subclass of the class java.lang.Throwable.

The class java.lang.Throwable inherits the class java.lang.Object.

If a method calls another method that may throw a checked exception, either it must be enclosed within a try-catch block or the method should declare this exception to be thrown in its method signature.

A runtime exception isn't a part of the method signature, even if a method may throw it.

A runtime exception may not necessarily be caught by a try-catch block.

An error need not be a part of a method signature.

Commonly occurring exceptions, categories, and classes

ArrayIndexOutOfBoundsException is a runtime exception that's thrown when a piece of code tries to access an array position out of its bounds.

IndexOutOfBoundsException is a runtime exception that's thrown when a piece of code tries to access a list position that's out of its bounds.

ArrayIndexOutOfBoundsException extends the class java.lang.IndexOutOfBoundsException, which extends the class java.lang.RuntimeException.

ClassCastException is a runtime exception. java.lang.ClassCastException extends java.lang.RuntimeException.

IllegalArgumentException is a runtime exception. java.lang.IllegalArgumentException extends java.lang.RuntimeException.

IllegalArgumentException is thrown to specify that a method has been passed illegal or inappropriate arguments.

NullPointerException is a runtime exception.

NumberFormatException is a runtime exception.