Chapter 6: exceptions Flashcards
A(n) ____ exception happens during runtime.
unchecked
What’s the difference between an exception and compiler error?
All exceptions happen at runtime. Compiler error happens before the program is run.
A runtime exception is also known as:
unchecked exception
A _____ exception inclucdes Exception and all subclassed that do not extend Runtime Exception
checked exception
Is this a checked or unchecked exception?
void fall() throws Exception{
throw new Exception();
}
Checked. Because we tell the program how to handle the exception be declaring.
What does the ‘throw’ keyword indicate when used in a program?
Just that this method might throw an exception
Is the NullPointerException checked or unchecked?
unchecked. We didn’t tell the program what to do and it happened at runtime.
How do you recognize a Runtime exception? What will it’s super class be?
RuntimeException
Does Java force you to explicit handle a runtime exception with something like a throws() or try catch?
No
How do you recognize a checked exception? What will it’s super class be?
It will be a subclass of Exception but not of RuntimeException.
Does Java force you to handle or declare a checked exception?
yes
How do you recognize an error exception? What will it’s superclass be?
Subclass of Error
Will the program catch an Error (are errors fatal?), and are you required to handle or declare an exception to handle an Error.
Errors are fatal. Errors are not declared.
What does Java use ‘try’ statements for?
To separate the logic that might throw an exception from the logic to handle that exception.
Are curly braces required in a try catch statement?
Yes.
What happens to the catch block of code if the code in the try block doesn’t fail?
It doesn’t run.
Are the curly braces in a try catch required around both the try block and the catch clause?
Yes
Why doesn’t this compile?
try
fall();
catch(Exceptione e)
System.out.print(“Get up”);
No. the try block requires curly braces
Does this compile?
try{
fall();
}
No. You must have a catch statement in a try/catch block.
When does the finally{} clause execute?
The finally clause executes no matter what in a try catch statement. If the try is successful, the finally still runs. If the try is unsuccessful and the catch clause runs, the finally block will still run.
Does finally have to follow a catch statement in a try catch block?
yes. if the catch doesn’t precede the finally the code will not compile.
Are you allowed to have multiple catch statements in a try/catch?
Yes.
Let’s say you have a try catch block with custom exceptions. Or, at least you are using different kinds of exceptions, some of which inherit from another. What happens if you run a subclass exception that runs before an exception using a superclass of that subclass as the exception.
This is just fine. As long as the subclass exception runs before the super this will be fine. but if the super runs before the sub, the compiler will indicate that some code is out of reach.
Remember, at most one catch block will run and it will be the first catch block that can handle it.
Which three types of exceptions do you need to be able to recognize for the OCA exam?
Runtime (unchecked) exceptions, check exceptions, and errors.
Do runtime exceptions have to be handled or declared?
No
Can runtime exceptions be thrown by both the programmer and the jvm?
yes
Why would ArithmeticException be thrown?
Thrown by the JVM when the code attempts to divide by zero
What causes an ArrayIndexOutofBoundsException to be thrown?
Thrown by the JVM when code uses an illegal index to access an array.
Why would a ClassCastException be thrown?
This is thrown by the JVM when an attempt is made to cast an object to a subclass of which it is not an instance.
Why would an IllegalArgumentException be thrown?
Thrown by the programmer to indicate a method has been passed an illegal or inappropriate argument.
What causes a NullPointerException?
This is thrown by the JVM when there is a null reference where an object is required.
Why would a NumberFormatException be thrown?
This is thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have the appropriate format.
Which exception would be thrown?
int answer = 11/0;
ArithmeticException
Which exception would be thrown by this code?
int[] countsOfMoose = new int[3];
System.out.println(counstOfMoose[-1]);
ArrayIndexOutofBoundsException
Which exception would be thrown by the code?
String type = “moose”;
Integer number = (Integer) type;
ClassCastException
Which exception would be thrown by this code?
String name;
public void printLength() {
System.out.println(name.length());
}
NullPointerException. name doesn’t have a value (other than null) so it cannot print out a length.
Which exception would be thrown by this code?
Integer.parseInt(“abc”);
NumberFormatException
Checked exceptions have Exception in their hierarchy but not _____
RuntimeException
Checked exceptions must be ____ or _____.
Handled or declared
Checked exceptions can be thrown by the _____ or the _____.
Checked exceptions can be thrown by the programmer or the jvm.
Which checked exception is thrown when code tries to reference a file that does not exist?
FileNotFoundException
Which exception is thrown programmatically when there’s a problem reading or writing a file?
IOException
FileNotFoundException is a child of ______
IOException
Errors extend the ____ class
Errors
Errors are thrown by the _____ and should not be _____ or ______.
Jvm. Handled or declared
Which error is thrown by the JVM when a static initializer throws an exception and doesn’t handle it?
ExceptionInInitializerError
Which error is thrown by infinite recursion?
StackOverflowError
Which error is thrown by the JVM when a class that the code uses is available at compile time but not at runtime?
NoclassDefFoundError
Java runs ____ initializers the first time a class is used.
Static initializers
Which two exceptions will this code throw?
static { int[] countsOfMoose = new int{3};
int num = countsOfMoose[-1];
}
public static void main(String[] args){}
ExceptionInInitializerError caused by ArrayIndexOutofBoundsException
Which exception will this code throw?
public static void whatever(int num){
whatever(1);
}
StackOverFlowError as this is infinite recursion
Why doesn’t this compile?
class CanNotHopException extends Exception{}
class Hopper{public void hop(){}}
class Bunny extends Hopper{
public void hop() throws CanNotHopException{}
}
Because Hopper doesn’t extend the CanNotHopException class. A class isn’t allowed to throw any checked exceptions unless the superclass declares them
Why wont this compile?
public static void main(String… strings) {
int[] numbers = {1,2,34};
int[] moreNumbers = new int[] {123,4,5,6};
int[] arraySize = new int[4];
try {
System.out.println(numbers[-1]);
} catch (Exception e ){
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e1) {
e1.printStackTrace();
}
}
Exception is the super class of ArrayIndexOutOfBoundsException, so if Exception is run, then the second code becomes unreachable. Java will find the first exception that will work. In this case the super will catch the problem.