<<< Java Exception Interview Questions and Answers >>>

Java Exception - Questions & Answers

Here is the list of Java Exception Interview Questions. The answers are short and to the point.These questions are frequently asked by the recruiters. So we try our best to provide you the Java Exception interview questions. A list of frequently asked Java Exception interview questions and answers are given below.

«« | 1 2 | »»

Exception: Java Interview Questions

1) What is exception in Java?

Ans : An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. The exception is said to be thrown whenever an exceptional event occurs in java which signals that something is not correct with the code written and may give unexpected result.Exception can arise from different kind of situations such as wrong data entered by user, hardware failure, network connection failure etc.

2) What is error?

Ans :An Error indicates that a non-recoverable condition has occurred that should not be caught. Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself.

3) Which is superclass of Exception?

Ans :"Throwable", the parent class of all exception related classes.

4) What are the advantages of using exception handling?

Ans :Exception handling provides the following advantages :

  1. Separating Error Handling Code from "Regular" Code.
  2. Propagating Errors Up the Call Stack.
  3. Grouping Error Types and Error Differentiation.

5) What are the types of Exceptions in Java?

Ans :There are two types of exceptions in Java :

  1. Checked Exceptions or runtime exception
  2. Unchecked Exceptions

Checked Exceptions :Exception occur during runtime is called checked exception. Checked Exception should be thrown with keyword "throws" or should be provided try catch block else program would not compile,we do get compilation error.
Some of the examples of checked exceptions are :

  1. IO exception
  2. File not found exception
  3. Class not found exception

Unchecked Exceptions :All subclasses of runtime exception are called unchecked exception.These are unchecked exception because compiler does not check if a method handles or throws exception.
Some of the examples of unchecked exceptions are :

  1. Arthmetic exception
  2. Array index out of bound exception
  3. Null pointer exception

6) Exceptions are defined in which java package?

Ans : All the exceptions are subclasses of java.lang.Exception.

7) Explain Java Exception Hierarchy?

Ans :

8) What are important methods of Java Exception Class?

Ans : Exception and all of it’s subclasses doesn’t provide any specific methods and all of the methods are defined in the base class Throwable.

  1. String getMessage()
  2. String getLocalizedMessage()
  3. synchronized Throwable getCause()
  4. String toString()
  5. void printStackTrace()

9) What are the different ways to handle exceptions?

Ans :There are two ways to handle exceptions :

  1. Wrapping the desired code in a try block followed by a catch block to catch the exceptions.
  2. List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.

10) What are 5 exception handling keywords in java?

Ans :There are 5 keywords used in java exception handling.

  1. Try
  2. Catch
  3. Throw
  4. Throws
  5. Finally

«« | 1 2 | »»