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.