Chapter 6 - Exceptions Flashcards
Exception Hierarchy
5
java. lang.Object
java. lang.Throwable
java. lang.Exception java.lang.Error
java. lang.RuntimeException
Unchecked Exceptions
6
all RuntimeExceptions e.g.: JVM - ArithmeticException JVM - ArrayIndexOutOfBoundsException JVM - ClassCastException Programmer - IllegalArgumentException JVM - NullPointerException Programmer - NumberFormatException
Checked Exceptions
2
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
Errors
3
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
Unreachable Code
1
private static void eatCarrot() throws NoMoreCarrotsException { }
public void bad() { try { eatCarrot(); } catch (NoMoreCarrotsException e ) {// DOES NOT COMPILE System.out.print("sad rabbit"); } }
Exceptions: Subclasses
1
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
Printing an Exception
3
System.out.println(e);
System.out.println(e.getMessage());
e.printStackTrace();
Understand the flow of a try statement
1
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.