Chapter 11: Exceptions Flashcards
What is an exception?
An exception is an event that alters program flow.
What superclass (aside from Object) do all exceptions inherit from in Java?
java.lang.Throwable
What is an Error and what should it be used for?
Error means something went so horribly wrong that your program should not attempt to recover from it. For example, the disk drive “dissapeared” or the program ran out of memory.
What is a checked exception?
A checked exception is an exception that must be declared or handled by the application code where it is thrown.
What do all checked exceptions inherit from?
Checked exceptions all inherit Exception (and thus also Throwable & Object)
What is an unchecked exception?
An unchecked exception is any exception that does not need to be declared or handled by the application code where it is thrown.
They are also known as runtime exceptions.
What do all unchecked exceptions inherit from?
Unchecked exceptions all inherit from either RuntimeException or Error.
What is the difference between throw and throws?
Throw is used as a statement inside a code block to throw a new exception.
Throws is used at the end of a method declaration to indicate what exception it might throw.
Is the following code valid?
Exception e = new RuntimeException(); throw e;
Yes.
An Exception inherits from the Object class and can therefore be stored in a variable.
Is the following code valid?
throw RuntimeException();
No. It is missing the new keyword and is thus never instantiated.
Is it okay for a program to catch an Error?
No. An error is not supposed to be recovered from.
When is the ArithmeticException thrown?
Is it checked or unchecked (runtime)?
Thrown when code attempts to divide by zero.
Unchecked.
When is the ArrayIndexOutOfBoundsException thrown?
Is it checked or unchecked (runtime)?
Thrown when code uses an illegal index to access an array.
Unchecked.
When is the ClassCastException thrown?
Is it checked or unchecked (runtime)?
Thrown when an attempt is made to cast an object to a class of which it is not an instance.
Unchecked.
When is the NullPointerException thrown?
Is it checked or unchecked (runtime)?
Thrown when there is a null reference where an object is required.
Unchecked.
When is the IllegalArgumentException thrown?
Is it checked or unchecked (runtime)?
Thrown by the programmer to indicate that a method has been passed an illegal or inappropriate argument.
Unchecked.
When is the NumberFormatException thrown?
Is it checked or unchecked (runtime)?
Subclass of IllegalArgumentException thrown when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format.
Unchecked.
When is the IOException thrown?
Is it checked or unchecked (runtime)?
Thrown programmatically when there is a problem reading or writing a file.
Checked.
When is the FileNotFoundException thrown?
Is it checked or unchecked (runtime)?
Subclass of IOException thrown programatically when code tries to reference a file that doesn’t exist.
Checked.