Chapter 6 Flashcards
Checked Exceptions
Checked exceptions have Exception in their hierarchy but not RuntimeException. They
must be handled or declared. They can be thrown by the programmer or by the JVM.
Common runtime exceptions include the following:
FileNotFoundException Thrown programmatically when code tries to reference a fi le
that does not exist.
IOException Thrown programmatically when there’s a problem reading or writing a fi le
For the OCA exam, you only need to know that these are checked exceptions.
Errors
Errors extend the Error class. They are thrown by the JVM and should not be handled or
declared. Errors are rare, but you might see these:
ExceptionInInitializerError Thrown by the JVM when a static initializer throws
an exception and doesn’t handle it
StackOverflowError Thrown by the JVM when a method calls itself too many times
(this is called infinite recursion because the method typically calls itself without end)
NoClassDefFoundError Thrown by the JVM when a class that the code uses is available
at compile time but not runtime
Runtime Exceptions
AKA Unchecked Exceptions. Runtime exceptions extend RuntimeException. They don’t have to be handled or declared.
They can be thrown by the programmer or by the JVM. Common runtime exceptions
include the following:
ArithmeticException Thrown by the JVM when code attempts to divide by zero
ArrayIndexOutOfBoundsException Thrown by the JVM when code uses an illegal
index to access an array
ClassCastException Thrown by the JVM when an attempt is made to cast an exception to a subclass of which it is not an instance
IllegalArgumentException Thrown by the programmer to indicate that a method has
been passed an illegal or inappropriate argument
NullPointerException Thrown by the JVM when there is a null reference where an
object is required
NumberFormatException Thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format
Checked Vs. Unchecked Exceptions
Runtime exceptions are also known as unchecked exceptions.
A checked exception includes Exception and all subclasses that do not extend
RuntimeException. Checked exceptions tend to be more anticipated—for example, trying
to read a fi le that doesn’t exist.
ArithmeticException
Unchecked (Runtime) Exception
Thrown by the JVM when code attempts to divide by zero
ArrayIndexOutOfBoundsException
Unchecked (Runtime) Exception
Thrown by the JVM when code uses an illegal index to access an array
ClassCastException
Unchecked (Runtime) Exception
Thrown by the JVM when an attempt is made to cast an exception to a subclass of which it is not an instance
IllegalArgumentException
Unchecked (Runtime) Exception
Thrown by the programmer to indicate that a method has been passed an illegal or inappropriate argument
NullPointerException
Unchecked (Runtime) Exception
Thrown by the JVM when there is a null reference where an object is required
NumberFormatException
Unchecked (Runtime) Exception
Thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format
FileNotFoundException
Checked Exception
Thrown programmatically when code tries to reference a file that does not exist.
IOException
Checked Exception
Thrown programmatically when there’s a problem reading or writing a file.