10 Exception Handling Interview Questions for Java

1) If the overridden method in super class A throws FileNotFoundException, then the overriding method present in class B which is a subclass of class A can throw IOException. If the above statement true?
Ans: The overriding method can not throw any checked exception other than the exception classes or sub-classes of those classes which are thrown by the overridden method.

In the scenario described in question, the method in class B can not throw IOException but can throw FileNotFoundException exception.

2) What is the difference between checked and unchecked exception handling in Java? What are the disadvantages of checked exception handling?
Ans: Checked exceptions are the one for which there is a check by the compiler that these exceptions have to be caught or specified with throws keyword. These kind of exceptions occur because of conditions which are out of control of the application like Network error, File Access Denied etc.

Unchecked exceptions are the one which arise because of logic written by the developer. e.g. Trying to access an array element which doesn’t exist.

3) If there is common code to be executed  in the catch block of 10 exceptions which are thrown from a single try block, then how that common code can be written with minimum effort?

Ans: In pre JDK 7, a method can be written and all catch blocks can invoke this method containing the common code.

In JDK 7, the | operator can be used in catch block in order to execute common code for multiple exceptions. e.g. catch(SQLException sqle | IOException ioe){}

4) Have you every created custom exceptions? Explain the scenario?

Ans: Custom exceptions are useful when the JDK exception classes don’t capture the essence of erroneous situation which has come up in the application. A custom exception can be created by extending any subclass of Exception class or by implementing Throwable interface.

E.g. A custom exception can be thrown when the custom JSP tag has value for the attribute which is not expected.

5) What is the difference between Validation, Exception and Error?
Ans: Validation is the process of making user enter data in a format which the application can handle.

Exception handling is the process when the application logic didn’t work as expected by the Java compiler.

Error occurs in a situation where the normal application execution can not continue. For e.g. out of memory.

6) What is the purpose of finally block? In which scenario, the code in finally block will not be executed?

Ans: finally block is used to execute code which should be executed irrespective of whether an exception occurs or not. The kind of code written in a finally block consists of clean up code such as closing of database/file connection.

But JDK 7 has come up with try with resources block which automatically handles the closing of resources.

7) What do you understand by Exception chaining?
Ans: I have written a blog post for this answer here

8) How will you make the client code to be transparent from business logic exceptions?
Ans: The use of Business Delegate design pattern makes the client invisible from business logic exceptions. This helps in decoupling the client from server which helps in better maintainability of the application in terms of future changes in client or business logic code.

9) What are the differences between NoClassDefFoundError and ClassNotFoundException?

Ans: NoClassDefFoundError occurs when a class was found during compilation but could not be located in the classpath while executing the program.

For example: class A invokes method from class B and both compile fine but before executing the program, an ANT script deleted B.class by mistake. Now on executing the program NoClassDefFoundError is thrown.

ClassNotFoundException occurs when a class is not found while dynamically loading a class using the class loaders.

For example: The database driver is not found when trying to load the driver using Class.forName() method.

10) What is the purpose of throw keyword? What happens if we write “throw null;” statement in a Java program?

Ans: ”throw” keyword is used to re-throw an exception which has been caught in a catch block. The syntax is “throw e;” where e is the reference to the exception being caught. The exception is re-thrown to the client.

This keyword is useful when some part of the exception is to be handled by the caller of the method in which throw keyword is used.

The use of “throw null;” statement causes NullPointerException to be thrown.

The answers are brief. Do post your comments if you think I have missed anything or discuss any other exception handling question.

Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email

Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email