Exceptions Flashcards
What is the difference between final, .finalize(), and finally?
Finally is used to place important code, it will be executed whether exception is handled or not. Finalize is used to perform clean up processing just before object is garbage collected. Final is a keyword. Finally is a block.
Explain throw vs throws vs Throwable
throws: Used when writing methods, to declare that the method in question throws the specified (checked) exception.
throw: Instruction to actually throw the exception. (Or more specifically, the Throwable). The throw keyword is followed by a reference to a Throwable (usually an exception).
Do you need a catch block?
Can you have more than 1?
Is there an order to follow?
Yes, we can have try without catch block by using finally block. You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.
What is base class of all exceptions? What interface do they all implement?
Throwable: The Throwable class is the superclass of all errors and exceptions in the Java language
List some checked and unchecked exceptions?
checked exceptions:
IOException
SQLException
ParseException
unchecked exceptions:
NullPointerException.
ArrayIndexOutOfBoundsException.
ArithmeticException.
IllegalArgumentException.
NumberFormatException.
Multi-catch block - can you catch more than one exception in a single catch block?
In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.