Ch 6 - Exceptions Flashcards

1
Q
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);
A

tfa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

true/false, the following code compiles:
try {
fall();
}

A

false. The catch clause is missing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is the “Exception” class a checked or unchecked exception?

A

checked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
true/false, the following code compiles:
try
fall();
catch (Exception e)
System.out.println("get up");
A

false. The braces are missing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are known as unchecked exceptions?

A

Runtime exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

true/false, a “finally” block can be used in places other than in a try/catch statement.

A

false. They can only be used as part of a try statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
true/false, the following custom exceptions are checked exceptions:
class AnimalsOutForWalk extends RuntimeException {}
class ExhibitClosed extends RuntimeException {}
class ExhibitClosedForLunch extends ExhibitClosed ()
A

false. They are all unchecked (Runtime) exceptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

true/false, you can have a “try” clause without a “catch” clause, so long as you include a “finally” clause.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the name of the super class that java uses to represent all events that alter program flow?

A

Throwable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
true/false, the following code compiles:
void explore() {
try {
seeAnimals();
fall();
} catch (Exception e) {
getHugFromDaddy();
} finally {
seeMoreAnimals();
}
goHome();
}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What keyword declares that the method might throw an exception?

A

throws

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What statement is used to separate the logic that might throw an exception from the logic to handle that exception?

A

try

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
true/false, the following code compiles:
void explore() {
try {
seeAnimals();
fall();
} finally {
seeMoreAnimals();
} catch (Exception e) {
getHugFromDaddy();
}
goHome();
}
A

false. The catch and finally clauses are in the wrong order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does System.exit(0) do?

A

It ends the program’s execution immediately.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What other type, besided Runtime exceptions, are there?

A

Checked exceptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What type of exception is “NullPointerException” an example of?

A

RuntimeException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
true/false, the following code compiles:
void explore() {
try {
seeAnimals();
fall();
} finally {
seeMoreAnimals();
}
goHome();
}
A

true. You can have a try without a catch, so long as you have a finally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Is the “RuntimeException” class a checked or unchecked exception?

A

unchecked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

true/false, the order in which exceptions are caught does not matter.

A

false. If it is impossible for one of the catch blocks to be executed, a compiler error about unreachable code occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
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");
	}
}
A

false. Since ExhibitClosedForLunch is a subclass of ExhibitClosed, the second catch block is unreachable and therefore will not compile.

21
Q
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");
	}
}
A

false. ExhibitClosed is a subclass of RuntimeException, so the second catch block will never be reached, and thus will generated a compiler error.

22
Q
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");
	}
}
A

true. Since ExhibitClosedForLunch is a subclass of ExhibitClosed, each of the catch blocks are reachable.

23
Q
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");
	}
}
A

true.

24
Q

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?

A

NumberFormatException

25
Q

Which exception is thrown by JVM when code uses an illegal index to access an array?

A

ArrayIndexOutOfBoundsException

26
Q

Which exception is thrown by JVM when an attempt is madeto cast an object to a subclass of which it is not an instance?

A

ClassCastException

27
Q

Which exception is thrown by JVM when code attempts to divide by zero?

A

ArithmeticException

28
Q

Which exception is thrown by JVM when there is a null reference where an object is required?

A

NullPointerException

29
Q

Which exception is thrown by programmer to indicate that a method has been passed an illegal or inappropriate argument?

A

IllegalArgumentException

30
Q

Which exception is thrown programmatically when code tries to reference a file that doesn’t exist?

A

FileNotFoundException - checked exception thrown by programmer or JVM

31
Q

Which exception is thrown programmatically when there’s a problem reading or writing a file?

A

IOException - checked exception thrown by programmer or JVM

32
Q

Which exception is thrown by the JVM when a static initialzier throws and exception and doesn’t handle it?

A

ExceptionInitializerError

33
Q

Which exception is thrown by the JVM when a method calls itself too many times?

A

StackOverflowError

34
Q

Which exception is thrown by the JVM when a class that the code uses is available at compile time but not runtime?

A

NoClassDefFoundError

35
Q
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;
}
A

before catch finally done

36
Q
What happens in the code block in the catch?
try {
	throw new RuntimeException();
} catch(RuntimeException e) {
	throw new RuntimeException();
} finally {
	throw new Exception();
}
A

The exception thrown in the catch block essentially gets ignored because of the presence of the finally block.

37
Q
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 {
	}
}
A

true

38
Q
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 { }
}
A

true

39
Q

Which two runtime exceptions are typically thrown by the programmer?

A

IllegalArgumentException and NumberFormatException

40
Q
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 {
	}
}
A

true

41
Q
true/false, the following code compiles:
class CanNotHopException extends Exception { }
class Hopper {
	public void hop() throws CanNotHopException { }
}
class Bunny extends Hopper {
	public void hop() { }
}
A

true

42
Q
true/false, the following code compiles:
class CanNotHopException extends Exception { }
class Hopper {
	public void hop() { }
}
class Bunny extends Hopper {
	public void hop() throws IllegalstateException { }
}
A

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.

43
Q
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 {
	}
}
A

false. NoMoreCarrotsException is a checked exception, and those must be handled or declared.

44
Q
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 { }
A

true.

45
Q

Name two common checked exceptions.

A

IOException and FileNotFoundException

46
Q
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() { }
A

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.

47
Q
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 { }
}
A

true

48
Q
true/false, the following code compiles:
class CanNotHopException extends Exception { }
class Hopper {
	public void hop() { }
}
class Bunny extends Hopper {
	public void hop() throws CanNotHopException { }
}
A

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.

49
Q

true/false, if a try statement has multiple catch blocks, at most one catch block can run.

A

true.