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
What can throw ArithmeticException?
Dividing by 0
What are Helpful NullPointerExceptions?
It is a new feature of NullPointerExceptions, where you can get detailed information about parameters that were reason of this exception. It shows number of parameter that threw it.
How can we get name of params instead of numbers in Helpful NullPointerExceptions?
-g:vars command at java startup
Name some Error exceptions
NoClassDefFoundError, ExceptionInitializerError, StackOverflowError
Name some checked Exceptions
SQLException, ParseException, FileNotFoundException, IOException, NotSerializableException
What exceptions extend IOException?
FileNotFoundException, NotSerializableException
What is the rule about resources closure in try-with-resources section?
They are being closed from last to first
What is the rule about resources that can be declared in try-with-resources section?
Classes must implement AutoCloseable interface
What is the scope of try-with-resources?
Only in try block
Can var be used in try-with-resources section?
Yes
Can resources be declared outside try-with-resources section?
Yes, but they need to be final or effectively final
Do try-with-resources need catch?
No, it is being implicitly created by compiler
What do we need to watch out when using try-with-resources?
Not to close resources that are needed later, or already closed
What are stacked or suppressed exceptions?
If multiple exceptions are occurred while throwing first exception, first exception is main exception, and others are considered suppressed but readable
When are resources being closed when using try-with-resources?
Right after try block ends
What is implicit finally block in try-with-resources?
It is a finally block that compiler adds right after try and catch blocks and before explicit finally block. It contains generated resource close calls. It is ran before finally block.
What happens if run a method that throws an exception, but as a field?
Then classes constructor needs to me marked that it throws that exception