Exceptions Flashcards

1
Q

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

A

.finalize() is a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection to perform clean-up activity. Final is a keyword meaning that the entity declared as final cannot be modified in the future. Finally is the last component of a try-catch-finally block. It executes regardless of what happens in the try or catch blocks.

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

Explain throw vs throws vs Throwable

A

Throwable is a superclass of all errors and exceptions. Throw and throws are keywords. Throw allows you to create a custom method and throw that exception from any keyword. Throws is used in the method signature to indicate that this method may throw mentioned exceptions.

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

Explain Throwable

A

Throwableis a super class for all types of errors and exceptions in java. This class is a member ofjava.langpackage. Only instances of this class or it’s sub classes are thrown by the java virtual machine or by the throw statement. The only argument of catch block must be of this type or it’s sub classes.

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

Explain Throw

A

Throwis a keywordin java which is used to create a custom exception. Using throw keyword, you can throw an exception from any method or block. But, that exception mustbe of typejava.lang.Throwableclass or it’s sub classes.

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

Throws

A

Throwsis also a keyword in java which is used in the method signature to indicate that this method may throw mentioned exceptions. The caller to such methods must handle the mentioned exceptions either using try-catch blocks or using throws keyword.

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

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

A

You can have 0, 1, or multiple catch blocks. If you have a catch block then it needs to go in between the try and finally block.

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

What is base class of all exceptions? What class do they all extend?

A

Exceptions is the base class of all exceptions. They all extend the throwable class.

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

List some checked exceptions

A

Checked/Compile time exceptions are caught at compilation. Java will not compile the program until the exception is handled, either with a try catch block or by using the throws keyword in the method signature.
Some checked/compile time exceptions include: FileNotFound, ClassNotFound, and NoSuchMethod

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

List some unchecked exceptions:

A

Unchecked/Runtime exceptions are exceptions that get through the compilation. They can also be handled with a try catch block, or by throwing them.
Some examples include ArithmeticException, IndexOutofBounds, and ArrayStoreException.

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

Can you catch more than one exception in a single catch block?

A

Yes, this is known as a multi-catch block. Starting from Java 7.0, it is possible for a single catch block to catch multiple exceptions by separating each with | (pipe symbol) in the catch block.

Catching multiple exceptions in a single catch block reduces code duplication and increases efficiency. The bytecode generated while compiling this program will be smaller than the program having multiple catch blocks as there is no code redundancy.

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