Chapter 6 - Exceptions Flashcards Preview

Java OCA Exam > Chapter 6 - Exceptions > Flashcards

Flashcards in Chapter 6 - Exceptions Deck (8)
Loading flashcards...
1
Q

Exception Hierarchy

5

A

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

2
Q

Unchecked Exceptions

6

A
all RuntimeExceptions e.g.:
JVM - ArithmeticException
JVM - ArrayIndexOutOfBoundsException
JVM - ClassCastException
Programmer - IllegalArgumentException
JVM - NullPointerException
Programmer - NumberFormatException
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
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

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

Printing an Exception

3

A

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

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.