65 Java Exception Handling Interview Questions Answers

Here, we have listed the most important exception handling interview questions in Java with the best possible answers.

These interview questions are generally asked in Java interviews by the interviewer.

If you nicely prepare answers to these exception handling interview questions in Java then definitely, you can easily answer questions asked by the interviewer.

Java Exception Handling Interview Questions and Answers


1. What is Exception in Java?

Ans: An exception is an error that affects the normal execution of program. If an exception is not handled, the program abruptly terminates.

2. What is the super or base class of all exceptions in Java?

Ans: Exception is the superclass of all exceptions in Java.

3. What is the superclass for error and exception classes in Java?

Ans: Throwable is the superclass for error and exception classes in Java.

4. What are the types of exceptions in Java API?

Ans: There are two types of exceptions available in Java API. They are:

a. Predefined Exceptions (Built-in-Exceptions): Predefined exceptions are those exceptions that are already defined by Java system. These exceptions are also called built-in-exceptions.

The built-in-exception is further divided into two categories: checked and unchecked exceptions.

b. Custom Exceptions: Custom exceptions are those exceptions that are created by users or programmers according to their own needs. The custom exceptions are also called user-defined exceptions that are created by extending the exception class.

5. Why an exception occurs in the program?

Ans: There can be many reasons that might generate an exception in a Java program.

  • Opening a non-existing file in your program.
  • Reading a file from a disk but the file does exist there.
  • Writing data to a disk but the disk is full or unformatted.
  • When the program asks for user input and the user enters invalid data.
  • When a user attempts to divide an integer value by zero, an exception occurs.
  • When a data stream is in an invalid format, etc.

6. What is Exception handling in Java?

Ans: The mechanism of handling unexpected errors in a java program is called exception handling. It is a powerful mechanism to handle runtime errors, ClassNotFoundException, FileNotFoundException, IOException, etc. so that the normal execution flow of the program can be maintained.

7. What is exception handler in Java?

Ans: The code that catches the exception thrown by JVM is called exception handler in Java. In other words, an exception handler is a block of code that performs an action when an exception occurs in a program.


8. What is the advantage of using exception handling in Java?

Ans: There are several advantages of using exception handling in java. They are:

  • The main advantage of exception handling technique is to maintain the normal flow of the program.
  • It provides flexibility in handling situations of errors.
  • It allows us to define a user-friendly message to handle the exception.
  • It helps to separate “Error-Handling code” from “Regular code.”

9. Which of the following statements will throw an exception?
a. System.out.println(1/0);
b. System.out.println(2.0/0);

Ans: a will throw an exception named ArithmeticException. The second statement will give output infinity.

10. Which exception may be thrown if the given code is executed?

a.

public class Test 
{
public static void main(String[] args) 
{
int[ ] list = new int[4];
System.out.println(list[4]);
  }
}

Ans: ArrayIndexOutOfBoundsException will be thrown if the above code is executed.

b.

public class Test 
{
public static void main(String[] args) 
{
int a = 20;
int b = 30;
int c = 10;
int x = (a * b)/(a - b + c);
System.out.println("Result: " +x);
  }
}

Ans: ArithmeticException will be thrown if code is executed.

11. Explain the Java exception hierarchy.

Ans: The hierarchy of exception in Java is shown in the below diagram.

Exception handling in Java interview questions

Throwable class is the parent class of all exception types. It is an immediate subclass of the Object class. Below Throwable class, there are two subclasses (two child objects) Error and Exception.

Error: Error class is the subclass of Throwable class and a superclass of all the runtime error classes. It terminates the program if there is a problem related to a system or resources (JVM).


Exception: An exception is an abnormal condition that is caused by runtime error in the program. It is the superclass of all exceptions in Java. It is further divided into checked and unchecked (runtime) exceptions.

The exception class hierarchy has been shown in the below figure.

Java exception handling interview questions for exception class hierarchy

Checked exceptions are those exceptions that are checked by Java compiler at compilation. A list of some important checked exceptions are given below:

  • ClassNotFoundException
  • InterruptedException
  • InstantiationException
  • IOException
  • SQLException
  • IllegalAccessException
  • FileNotFoundException, etc

Runtime exceptions are those exceptions that are checked by JVM at runtime. Some important examples of runtime exceptions are given below:

  • ArithmeticException
  • ClassCastException
  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • NegativeArraySizeException
  • ArrayStoreException
  • IllegalThreadStateException
  • SecurityException, etc.

12. What does JVM do when an exception occurs in a program?

Ans: When JVM faces an exception in a program, it creates an exception object and throws it to inform us that an error has occurred. If the exception object is not caught and handled properly, JVM will display an error message and will terminate the rest of the program abnormally.

13. How do we catch an exception?

Ans: We can catch an exception in either of two ways. They are:

  • By try-catch block
  • By using throws clause

14. What is throwing an exception in Java?

Ans: When an exception occurs inside a method in java program, the method in which exception has occurred creates an exception object (i.e, an object of exception class) internally with the help of JVM and hands it over to the java runtime system (JVM). This process is called throwing an exception in java.

15. What is catching an exception in Java?

Ans: The process of finding a handler by JVM to handle thrown exception is called catching an exception.

16. What will happen to exception object after exception handling is done?

Ans: Once exception handling is done, the exception object will be garbage collected.

17. What is the difference between checked and unchecked exceptions in Java?

Ans: Refer to answer to question 11.

18. How will you handle the checked exception?
Or What are the different ways to handle checked exceptions?

Ans: A checked exception can be handled either by using try and catch block or by using throws clause in the method declaration. If not handles properly, it will give a compile-time error.

19. Which exception class can you use in the catch block to handle both checked and unchecked exceptions?

Ans: Exception class

20. Can we throw checked exceptions from the static block?

Ans: We cannot throw because there is no specific place to catch it and it is called only once.

Please share your love