Ch 6 - Exceptions Flashcards
What is the output of the following code? String s = ""; try { s += "t"; } catch (Exception e) { s += "c"; } finally { s += "f"; } s += "a"; System.out.print(s);
tfa
true/false, the following code compiles:
try {
fall();
}
false. The catch clause is missing.
Is the “Exception” class a checked or unchecked exception?
checked
true/false, the following code compiles: try fall(); catch (Exception e) System.out.println("get up");
false. The braces are missing.
What are known as unchecked exceptions?
Runtime exceptions
true/false, a “finally” block can be used in places other than in a try/catch statement.
false. They can only be used as part of a try statement.
true/false, the following custom exceptions are checked exceptions: class AnimalsOutForWalk extends RuntimeException {} class ExhibitClosed extends RuntimeException {} class ExhibitClosedForLunch extends ExhibitClosed ()
false. They are all unchecked (Runtime) exceptions.
true/false, you can have a “try” clause without a “catch” clause, so long as you include a “finally” clause.
true
What is the name of the super class that java uses to represent all events that alter program flow?
Throwable
true/false, the following code compiles: void explore() { try { seeAnimals(); fall(); } catch (Exception e) { getHugFromDaddy(); } finally { seeMoreAnimals(); } goHome(); }
true
What keyword declares that the method might throw an exception?
throws
What statement is used to separate the logic that might throw an exception from the logic to handle that exception?
try
true/false, the following code compiles: void explore() { try { seeAnimals(); fall(); } finally { seeMoreAnimals(); } catch (Exception e) { getHugFromDaddy(); } goHome(); }
false. The catch and finally clauses are in the wrong order.
What does System.exit(0) do?
It ends the program’s execution immediately.
What other type, besided Runtime exceptions, are there?
Checked exceptions.
What type of exception is “NullPointerException” an example of?
RuntimeException
true/false, the following code compiles: void explore() { try { seeAnimals(); fall(); } finally { seeMoreAnimals(); } goHome(); }
true. You can have a try without a catch, so long as you have a finally.
Is the “RuntimeException” class a checked or unchecked exception?
unchecked
true/false, the order in which exceptions are caught does not matter.
false. If it is impossible for one of the catch blocks to be executed, a compiler error about unreachable code occurs.
true/false, the following code compiles: class ExhibitClosed extends RuntimeException {} class ExhibitClosedForLunch extends ExhibitClosed ()
public void visitMonkeys() { try { seeAnimal(); } catch (ExhibitClosed e) { System.out.println("not today"); } catch (ExhibitClosedForLunch e) { System.out.println("try back later"); } }
false. Since ExhibitClosedForLunch is a subclass of ExhibitClosed, the second catch block is unreachable and therefore will not compile.
true/false, the following code compiles: class ExhibitClosed extends RuntimeException {} class ExhibitClosedForLunch extends ExhibitClosed () public void visitSnakes() { try { seeAnimal(); } catch (RuntimeException e) { System.out.println("runtime exception"); } catch (ExhibitClosed e) { System.out.println("not today"); } catch (Exception e) { System.out.println("exception"); } }
false. ExhibitClosed is a subclass of RuntimeException, so the second catch block will never be reached, and thus will generated a compiler error.
true/false, the following code compiles: class ExhibitClosed extends RuntimeException {} class ExhibitClosedForLunch extends ExhibitClosed ()
public void visitMonkeys() { try { seeAnimal(); } catch (ExhibitClosedForLunch e) { System.out.println("try back later"); } catch (ExhibitClosed e) { System.out.println("not today"); } }
true. Since ExhibitClosedForLunch is a subclass of ExhibitClosed, each of the catch blocks are reachable.
true/false, the following code compiles: class AnimalsOutForWalk extends RuntimeException {} class ExhibitClosed extends RuntimeException {} class ExhibitClosedForLunch extends ExhibitClosed ()
public void visitPorcupine() { try { seeAnimal(); } catch (AnimalsOutForWalk e) { System.out.println("try back later"); } catch (ExhibitClosed e) { System.out.println("not today"); } }
true.
Which exception is thrown by programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format?
NumberFormatException
Which exception is thrown by JVM when code uses an illegal index to access an array?
ArrayIndexOutOfBoundsException
Which exception is thrown by JVM when an attempt is madeto cast an object to a subclass of which it is not an instance?
ClassCastException
Which exception is thrown by JVM when code attempts to divide by zero?
ArithmeticException
Which exception is thrown by JVM when there is a null reference where an object is required?
NullPointerException
Which exception is thrown by programmer to indicate that a method has been passed an illegal or inappropriate argument?
IllegalArgumentException
Which exception is thrown programmatically when code tries to reference a file that doesn’t exist?
FileNotFoundException - checked exception thrown by programmer or JVM
Which exception is thrown programmatically when there’s a problem reading or writing a file?
IOException - checked exception thrown by programmer or JVM
Which exception is thrown by the JVM when a static initialzier throws and exception and doesn’t handle it?
ExceptionInitializerError
Which exception is thrown by the JVM when a method calls itself too many times?
StackOverflowError
Which exception is thrown by the JVM when a class that the code uses is available at compile time but not runtime?
NoClassDefFoundError
What is the output of the following code? public String exceptions() { String result = ""; String v = null; try { try { result += "before "; v.length(); result += "after "; } catch (NullPointerException e) { result += "catch "; throw new RuntimeException(); } finally { result += "finally "; throw new Exception(); } } catch (Exception e) { result += "done"; } return result; }
before catch finally done
What happens in the code block in the catch? try { throw new RuntimeException(); } catch(RuntimeException e) { throw new RuntimeException(); } finally { throw new Exception(); }
The exception thrown in the catch block essentially gets ignored because of the presence of the finally block.
true/false, the following code compiles: class NoMoreCarrotsException extends Exception {} public class Bunny { public static void main(String[] args) throws NoMoreCarrotsException { eatCarrot(); } private static void eatCarrot() throws NoMoreCarrotsException { } }
true
true/false, the following code compiles: class CanNotHopException extends Exception { } class Hopper { public void hop() throws CanNotHopException { } } class Bunny extends Hopper { public void hop() throws CanNotHopException { } }
true
Which two runtime exceptions are typically thrown by the programmer?
IllegalArgumentException and NumberFormatException
true/false, the following code compiles: class NoMoreCarrotsException extends Exception {} public class Bunny { public static void main(String[] args) { try { eatCarrot(); } catch (NoMoreCarrotsException e) { System.out.println("sad rabbit!"); } } private static void eatCarrot() throws NoMoreCarrotsException { } }
true
true/false, the following code compiles: class CanNotHopException extends Exception { } class Hopper { public void hop() throws CanNotHopException { } } class Bunny extends Hopper { public void hop() { } }
true
true/false, the following code compiles: class CanNotHopException extends Exception { } class Hopper { public void hop() { } } class Bunny extends Hopper { public void hop() throws IllegalstateException { } }
true. The reason it’s ok to delcare a new runtime exception in a subclass method is that the declaration is redundant. Methods are free to throw any runtime exception they want without mentioning them in the method declaration.
true/false, the following code compiles: class NoMoreCarrotsException extends Exception {} public class Bunny { public static void main(String[] args) { eatCarrot(); } private static void eatCarrot() throws NoMoreCarrotsException { } }
false. NoMoreCarrotsException is a checked exception, and those must be handled or declared.
true/false, the following code compiles: public void method1() { try { eatCarrot(); } catch (NoMoreCarrotsException e) { System.out.println("sad rabbit!"); } } public void method2() throws NoMoreCarrotsException { eatCarrot(); } private static void eatCarrot() throws NoMoreCarrotsException { }
true.
Name two common checked exceptions.
IOException and FileNotFoundException
true/false, the following code compiles: public void method1() { try { eatCarrot(); } catch (NoMoreCarrotsException e) { System.out.println("sad rabbit!"); } } public void method2() throws NoMoreCarrotsException { eatCarrot(); } private static void eatCarrot() { }
false. Java knows that eatCarrot() can’t throw a checked exception, which means there’s no way for the catch block in method1() to be reached.
true/false, the following code compiles: class CanNotHopException extends Exception { } class Hopper { public void hop() throws Exception { } } class Bunny extends Hopper { public void hop() throws CanNotHopException { } }
true
true/false, the following code compiles: class CanNotHopException extends Exception { } class Hopper { public void hop() { } } class Bunny extends Hopper { public void hop() throws CanNotHopException { } }
false. Since the hop() method in Bunny is overriding the hop() method in Hopper, it’s not allowed to add new checked exceptions to the method signature.
true/false, if a try statement has multiple catch blocks, at most one catch block can run.
true.