Exceptions Flashcards

1
Q

What is the difference between final, .finalize(), and finally?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain throw vs throws vs Throwable

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do you need a catch block?
Can you have more than 1?
Is there an order to follow?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is base class of all exceptions? What interface do they all implement?

A

Throwable: The Throwable class is the superclass of all errors and exceptions in the Java language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List some checked and unchecked exceptions?

A

checked exceptions:

IOException
SQLException
ParseException

unchecked exceptions:

NullPointerException.
ArrayIndexOutOfBoundsException.
ArithmeticException.
IllegalArgumentException.
NumberFormatException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Multi-catch block - can you catch more than one exception in a single catch block?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly