Core Java : Exception Handling interview question

1. What is an exception?

An exception is an event, which occurs during the execution of a program, and disrupts the normal flow of the program’s instructions.

2. What is error?

An Error indicates a non-recoverable condition that cannot be caught by the programmer. Error includes   some drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself.

3. What are the advantages of using exception handling?

Exceptions provide the means to separate the details of what to do when something out of the ordinary happens from the main logic of a program. In traditional programming, error detection, reporting, and handling often lead to confusing spaghetti code.

  1. Separating Error-Handling Code from “Regular” Code.
  2. Propagating Errors Up the Call Stack.
  3. Grouping and Differentiating Error Types.

4. What are the types of Exceptions in Java?

There are two types of exceptions in Java:- unchecked exceptions and checked exceptions.

Checked exceptions: A checked exception is subclass of Exception  .Checked exceptions are checked at compile-time. It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error. It is named as checked exception because these exceptions are checked at Compile time. A checked is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

  • SQLException
  • IOException
  • DataAccessException
  • ClassNotFoundException
  • InvocationTargetException

Unchecked exceptions are not checked at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. Most of the times these exception occurs due to the bad data provided by .It is up to the programmer to judge the conditions in advance, that can cause such exceptions and handle them appropriately. All Unchecked exceptions are direct sub classes of RuntimeException class.

  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • ArithmeticException
  • IllegalArgumentException

5. Explain the significance of try-catch blocks?

Whenever the exception occurs in Java, we need a way to tell the JVM what code to execute. For this we need to create an exception handler .

constructing an exception handler is to enclose the code that might throw an exception within a try block. One or more catch clauses match a specific exception to a block of code that handles it..

If an exception occurs within the try block, that exception is handled by an exception handler associated with it. To associate an exception handler with a try block, we must put a catch block after it.

No code can be between the end of the try block and the beginning of the first catch block.

try {

} catch (Exception Type name) {

} catch (Exception Type name) {


6. What is the use of finally block?

creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement matches the exception. This is also used to close files, release network sockets, connections, and perform any other cleanup that code requires.



// code that can through Exception

}catch(ExceptionType1 e1)


//Catch block

}catch(ExceptionType2 e2)


//Catch block

}catch(ExceptionType3 e3)


//Catch block



// always executes.


7. What is the base class for Error and Exception?

Throwable is the base class for all Error and Exception.

8. Is it necessary that each try block must be followed by a catch block?

It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block.

9. What is difference between throw and throws?

throw keyword throws keyword
1) throw is used to explicitly throw an exception. throws is used to declare an exception.
2) throw is followed by an instance. throws is followed by class.
3) throw is used within the method. throws is used with the method signature.
4)You cannot throw multiple exception You can declare multiple exception e.g. public void method()throws IOException,SQLException.


Leave a Reply

Your email address will not be published. Required fields are marked *

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