Chapter 6 - Exceptions Flashcards

1
Q

Exception Hierarchy

5

A

java. lang.Object
java. lang.Throwable
java. lang.Exception java.lang.Error
java. lang.RuntimeException

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

Unchecked Exceptions

6

A
all RuntimeExceptions e.g.:
JVM - ArithmeticException
JVM - ArrayIndexOutOfBoundsException
JVM - ClassCastException
Programmer - IllegalArgumentException
JVM - NullPointerException
Programmer - NumberFormatException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Checked Exceptions

2

A

Checked exceptions have Exception in their hierarchy but not RuntimeException. They must be handled or declared. They can be thrown by the programmer or by the JVM. Common runtime exceptions include the following:

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

Errors

3

A

Errors extend the Error class. They are thrown by the JVM and should not be handled or declared. Errors are rare, but you might see these:
JVM - ExceptionInInitializerError
JVM - StackOverflowError
JVM - NoClassDefFoundError

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

Unreachable Code

1

A

private static void eatCarrot() throws NoMoreCarrotsException { }

public void bad() { try {
  eatCarrot();
} catch (NoMoreCarrotsException e ) {// DOES NOT COMPILE
System.out.print("sad rabbit"); }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Exceptions: Subclasses

1

A
When a class overrides a method from a
superclass or implements a method from an interface, it’s not allowed to add new checked exceptions to the method signature
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Printing an Exception

3

A

System.out.println(e);
System.out.println(e.getMessage());
e.printStackTrace();

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

Understand the flow of a try statement

1

A

A try statement must have a catch or a finally block. Multiple catch blocks are also allowed, provided no superclass exception type appears in an earlier catch block than its subclass. The finally block runs last regardless of whether an exception is thrown.

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