Thursday, 1 September 2016

Top 10 Exception Handling Interview Questions

Excepting handling is very important topic for Java programming and for cracking any Java/J2EE interview also. I prepared this list of all important exception handling interview question. This will help you to answer any exception handling related question in interviews.

1.) What is Exception in Java?

Exception is any abnormal situation that occurs during program execution and disturb the normal flow of the execution.

2.) What is Exception hierarchy in Java?



Throwable - Throwable is the root of all Exception hierarchy in Java. All Errors and Exceptions comes under Throwable class. 

Error - Errors are irrecoverable situations which mostly occurs due to system failure. Some of the examples of Errors are like OutOfMemory or some hardware failure that can not be recovered and they are not related to application scope.

Exception - Exception is abnormal situation which can be handled by the application. Exception is further divided into two types. One is checked (Compile-time) exception and second is Unchecked(run-time) exceptions.

Checked Exceptions - Checked exceptions are checked by java compiler at compile time and if not handled in program then compiler will throw compile time error. Java compiler forces us to either catch the exception or declare it in the method signature in throws clause. Some of the checked exceptions are FileNotFoundException, SQLException and ClassNotFoundException etc. 

Unchecked Exceptions - Unchecked exceptions are not checked by compiler at compile time. unchecked exception mostly occurs because of bad coding practice. Some of the examples of unchecked exceptions are NullPointerException, ArrayIndexOutOfBoundException and ArithmeticException etc.

3.) How to handle exceptions in Java?

Exceptions in Java can be handled in two ways -

  1. By declaring in throws clause of method signature
  2. By using try-catch
Handling Exception by declaring in throws clause -
If we don't want to handle exception in our program then we can declare it in throws clause of method signature. When some exception is declared in throws clause it is actually propagated to the caller method where caller method can handle it or can declare again to throws clause to pass to his caller.
Multiple exceptions can be declared in throws clause.

Handling Exception using try-catch 
To handle exception in our program try-catch is used. Suspected code is wrapped in try block and after try immediately catch block attached. Catch block requires a Exception object.

So if any exception occurs in try block the control directly goes to catch block where handling code is written. There can be multiple catch block with a single try block. Try-catch block can be nested also.

4.) Does order matters while declaring multiple catch block with a try block?

Yes, we should always catch the sub class exceptions before super class exceptions. If we change the order compiler will throw unreachable code error. 

5.) What is finally ?

Finally block is used in exception handling with try-catch blocks. Finally block will always executed weather exception is occurred or not. The main purpose of finally block to write code for freeing the resource or connection used.

Finally can be written with try block directly, its not mandatory to have catch block between try and finally.

 try{

// Code which can throw exception
}catch (Exception e) {
// TODO: handle exception
}finally {
//close open connections and free the resources

}
Imp Note - Finally block will not be executed if System.exit(0) called or power off the system.

6.)  What is throw keyword ? What is the difference between throw and throws ?

Throw - throw keyword is used to throw some exception in some specific situation.

So throw is used to throw some exception while throws is used declare the exception which we are not going to handle in program and they will propagated to the caller of the method.

7.) How to create a custom exception class in Java?

We can write a custom exception by extending Exception or any of its sub class. Below is a sample custom exception class.


package javaprinciples.exceptions;

public class CustomException extends Exception {

private static final long serialVersionUID = 1L;

public CustomException(String message) {
super(message);
}

}

8.) What is difference between final, finally and finalize in Java ?

final - final keyword is used with classes, methods or variables. final classes can not be extended. final methods can not be overridden and value of final variable can not be changed.

finally - finally keyword is used in exception handling to write some code which will be executed whether any exception is thrown or not.

finalize - finalise is a method which is used to write code to free the resources or make the objects again reachable. finalise is called before the object is garbage collected.

9.) What is multi catch feature added in Java7 ?

Prior Java7 only one exception can be caught in one catch block. In Java7 new feature was added so now we can catch multiple exception in a single catch block which can be separated by "|" to declare.

Sample code -


try {
    // execute code that may throw any exception from below 3.
} catch(SQLException | IOException e) {
    //Exception handling code
} catch(Exception e) {
//Exception handling code

}

10.) What is try-with-resource feature in exception handling ?

try-with-resource feature was introduced in Java7 to make code easy and error free. Before Java7 we always needs to write code in finally block to make the resources free after execution. 

But now we can write code using try-with-resource where we create resource object in try statement and use it in the try block. We don't need to write code for closing the resources in finally. JRE will automatically close the resource after execution completion.

Sample code -

try (AnyResource resource = new AnyResource()) {
            // use resource here
        } catch (Exception e) {
            // Exception handling code
        }

So these are the important questions from exception handling in Java. I hope this will help you. If you have any doubts please put in comments, I will try to resolve ASAP.

Check below links also-
Top 20 Collection Framework Interview Questions
Core Java Interview Questions - OOps and Basics
Core Java Interview Questions - String
Servlet Interview Questions
Bubble Sort Algorithm analysis and implementation in Java
Linear Search Algorithm analysis and implementation in Java
Binary Search Algorithm analysis and implementation in Java

No comments:

Post a Comment