Exception Handling Questions

0
640

Exception Handling in Java is highly important to understand. Reason being the concept is used through out the Java projects either a web project or a core Java one only. That’s why this section has its own importance. Here I am going to share TOP 10 Exception Handling Interview Questions and Answers. You must know all of these if you are going to be in any interview related to Java technology.

Question 1: What is the hierarchy of Exception Class in Java?

The Throwable class is the super class of all errors and exceptions in the Java language. There are two direct subclasses of Throwable : Error and Exception.

Question 2: What are Checked and Unchecked Exceptions and what are the different ways to handle them?

Checked exceptions are those which are checked at the compile time. They must be explicitly caught in a method or declared in the method’s throws clause.

Unchecked exceptions are the ones which are not checked at the compile time and are caused by problems that can not be solved, such as dividing by zero, null pointer, etc. Methods don’t explicitly have to declare that they can throw an unchecked exception and callers don’t have to handle them explicitly.
Example: IOException is a commonly used checked exception and RuntimeException is an unchecked exception.

Question 3: What is the difference between throws and throw?

We use ‘throws’ in method declarations where content of the method may throw an exception. In this way the responsibility to handle the exception occurred in method is passed to the caller method. Whereas, ‘throw’ keyword can be used in method bodies to explicitly throw an exception.

Question 4: How can we write our own exception in Java?

Java gives you the facility to create your own exception. To create a checked exception, you can subclass ‘Exception’ class or any of its subclass except RuntimeException and its child classes:

Class MyOwnException extends Exception {
}

You can create your own runtime exception by extending RuntimeException class.

Class MyOwnException extends RuntimeException {
}

Question 5: How many catch blocks can be there and is it mandatory to have a catch block for a try block?

There can be multiple catch blocks for one try block. It is not mandatory to have a catch block after try but one of the catch or finally block must be there after a try block.
The classic way to handle the exceptions is to have try-catch blocks but one can go with try-finally blocks as well, based on the need.

Question 6: What is difference between final, finally and finalize?

final keyword can be used along with variable, method and Class.
A final variable can not change it’s value and acts like a constant. final variables are initialized at the time of creation except in case of blank final variable which is initialized in Constructor.
A final method can not be overridden in any sub class .
A final class can not be sub classed and automatically makes all its method final.
final keyword also helps to write Immutable classes.

finally keyword gets used in terms of exception handling. finally block can be used with try and catch block. Even if catch block is not present, finally can be used with try and is certain to be executed.
finalize() is used in Garbage Collection concept and the method gets called just before collecting the objects eligible for collection. Though there is no certainty that this method would be called.

Question 7: What is the importance of having finally block?

As explained above, finally keyword gets used in terms of exception handling. finally block can be used with try and catch block. Even if catch block is not present, finally can be used with try and is certain to be executed.

Question 8: What is the difference between Error and Exception in Java?

An exception is an event, which occurs during the execution of a program and it disrupts the normal flow of the program’s instructions.
Error indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. Errors are out of the scope of application and it is not possible to handle them or recover them. For example: Hardware crash, JVM crash etc.

Question 9: How do we handle the exception?

Java provides three exception handler components — try, catch, and finally blocks
For more details please go through the article.

Question 10: How does a multi-catch block works?

In multi-catch block has been introduced in Java 7 using which we can catch multiple exceptions in a single catch block.

This feature helps reducing the code duplication and lessen the temptation to catch an overly broad exception.

Example :
Multiple catch blocks (Earlier)
catch (IOException ex){
logger.log(ex);
throw ex;
}
catch (SQLException ex){
logger.log(ex);
throw ex;
}

With multi catch (Now)
catch(IOException | SQLException ex)
{
logger.log(ex);
throw ex;
}

Previous articleAll About Constructors
Next articleCollection Interview Questions
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY