Exception Handling Questions Flashcards

1
Q

What is an exception?

A

Exceptions are abnormal conditions that arise during execution of the program. It may occur due to wrong user input or wrong logic written by programmer.

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

Which package has definitions for all the exception classes?

A

Java.lang.Exception

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

What are the types of exceptions?

A

Checked exceptions

Unchecked exceptions

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

What are checked exceptions?

A

These exceptions must be handled by programmer otherwise the program would throw a compilation error.

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

What are unchecked exceptions?

A

It is up to the programmer to write the code in such a way to avoid unchecked exceptions. You would not get a compilation error if you do not handle these exceptions. These exceptions occur at runtime.

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

What is the difference between Error and Exception?

A

Error: Mostly a system issue. It always occur at run time and must be resolved in order to proceed further.
Exception: Mostly an input data issue or wrong logic in code. Can occur at compile time or run time.

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

What is throw keyword in exception handling?

A

The throw keyword is used for throwing user defined or pre-defined exception.

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

What is throws keyword?

A

If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method’s signature.

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

Can static block throw exception?

A

Yes, A static block can throw exceptions. It has its own limitations: It can throw only Runtime exception (Unchecked exceptions), In order to throw checked exceptions you can use a try-catch block inside it.

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

What is finally block?

A

Finally block is a block of code that always executes, whether an exception occurs or not. Finally block follows try block or try-catch block.

Q) ClassNotFoundException vs NoClassDefFoundError?

1) ClassNotFoundException occurs when loader could not find the required class in class path.
2) NoClassDefFoundError occurs when class is loaded in classpath, but one or more of the class which are required by other class, are removed or failed to load by compiler.

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

Can we have a try block without catch or finally block?

A

No, we cannot have a try block without catch or finally block. We must have either one of them or both.

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

Can we have multiple catch blocks following a single try block?

A

Yes we can have multiple catch blocks in order to handle more than one exception.

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

Is it possible to have finally block without catch block?

A

Yes, we can have try block followed by finally block without even using catch blocks in between.

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

When does a finally block does not get executed?

A

The only time finally won’t be called is if you call System.exit() or if the JVM crashes first.

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

Can we handle more than one exception in a single catch block?

A

Yes we can do that using if-else statement but it is not considered as a good practice. We should have one catch block for one exception.

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