Exceptions Flashcards
What is parent class of all exception classes?
Throwable
What classes extend Throwable?
Exception and Error
What are checked exceptions?
Those exceptions that need to be declared or handled. They are expected and checked at compile time.
What are unchecked exceptions?
Exceptions that are optionally handled. They can appear at any time. They are not checked at compile time.
What main types of exceptions are unchecked exceptions?
Error, RuntimeException
What main types of exceptions are checked exceptions?
Throwable, Exception
Can you catch exceptions from methods that are empty?
Only if they are unchecked exceptions. Checked exceptions must be declared in method signature.
What do try catch need to have?
They must contain {}
How does “finally” work?
It executes before return, break, continue keywords in try or catch blocks, then it returns to block it came from.
When is catch block not necessary?
When you have finally block or you use try-with-resources
What is potential pitfall when using exceptions and its “throw” keyword?
Need to watch out for “throw” and “throws”
What is rule of overriding methods that throw a checked exception?
Overridden method can throw same exception, narrower exception or not declare exception at all. But it cannot declare different exception or broader exception.
Overridden method can throw narrower exception or not declare exception at all. Why?
Liskov Substitution Principle. Overridden method can potentially fix what throws a exception in super class, or can granulate problem into a subtype of the super method’s exception.
Name some RuntimeExceptions
NullPointerException, IllegalArgumentException, NumberFormatException, ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException
What exception does NumberFormatException extends?
IllegalArgumentException