Java Exception Interview Questions

In this article series, we will discuss different types of questions that can be asked in a Java interview, in order for the employer to test your skills in Java and object-oriented programming. This article is dedicated to Java Exceptions.


Java Exception Interview Questions

1What are the two types of Exceptions in Java? Which are the differences between them?

Java has two types of exceptions: checked exceptions and unchecked exceptions. Unchecked exceptions do not need to be declared in a method or a constructor’s throws clause, if they can be thrown by the execution of the method or the constructor, and propagate outside the method or constructor boundary. On the other hand, checked exceptions must be declared in a method or a constructor’s throws clause. See here for tips on Java exception handling.

2What is the difference between Exception and Error in java?

Exception and Error classes are both subclasses of the Throwable class. The Exception class is used for exceptional conditions that a user’s program should catch. The Error class defines exceptions that are not excepted to be caught by the user program.

3What is the difference between throw and throws?

The throw keyword is used to explicitly raise an exception within the program. On the contrary, the throws clause is used to indicate those exceptions that are not handled by a method. Each method must explicitly specify which exceptions does not handle, so the callers of that method can guard against possible exceptions. Finally, multiple exceptions are separated by a comma.

ALSO READ  Java 9 Released ! here are the notable features

4What is the importance of finally block in exception handling?

A final block will always be executed, whether or not an exception is actually thrown. Even in the case where the catch statement is missing and an exception is thrown, the final block will still be executed. The last thing to mention is that the final block is used to release resources like I/O buffers, database connections, etc.

5What will happen to the Exception object after exception handling?

The Exception object will be garbage collected in the next garbage collection.

6How does finally block differ from finalize() method?

A final block will be executed whether or not an exception is thrown and is used to release those resources held by the application. Finalize is a protected method of the Object class, which is called by the Java Virtual Machine (JVM) just before an object is a garbage collected.


General Java Interview Questions

Java Thread Interview Questions

Java Collections Interview Questions

Java Garbage Collectors Interview Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.