Java Exception Handling Interview Questions And Answers

Exceptional handling is one of the most important topics in core java.
Here is list of questions that may be asked on Exceptional handling.

Question 1: What is Exception ?

Answer:
  • Java doc says “ An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.”
  • The term “exception” means “exceptional condition” and is an occurrence that changes the normal program flow.
  • A bunch of things can lead to exceptions, including hardware failures, resource exhaustion, and good old bugs.
  • When an exception event occurs in Java , an exception is said to be “thrown”.
  • The code that is responsible for doing something about the exception is called an “exception handler” and it “catches” the thrown exception.
  • Every Exception will be thrown at runtime.

Question 2: How can you handle exception in java?

Answer:
try-catch-finally blocks are used to handle exception in java.
try : This block has code which can throw exception.
catch : This block is used to handle appropriate exception.
finally : This block is used to write any clean up code irrespective of whether any exception occurred or not.
Question 3: Explain the Exception hierarchy.
Exceptional Handling Hierarchy

Question 4: Difference between checked exception, unchecked exceptionand and errors

Parameter
Checked Exception
Unchecked Exception
Error
How to recognise
sub class of Exception class
sub class of RuntimeException class
sub class of Error class
Good to 
catch
Yes
Yes
No
Is Program required to handle or declared
Yes
No
No
Thrown by
Programatically
JVM
JVM
Recoverable
Yes
Yes
No
Example
IOException
FileNotFoundException etc
NullPointerException
ClassCastException
etc
StackOverFlowError
OutOfMemoryError
etc

 

Question 5: Can we have try without catch block in java ?

Answer:
Yes, we can have try without catch block by using finally block. You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit()..

Question 6: What is RunTime exception in java?

Answer:

RunTime exception is the exception which is thrown at run time. These exceptions occur due to programmatic errors and need to be corrected. Compiler is not aware of any such exception.
Example:
If you are working in java for quite some time, you might have got NullPointerException.

Let’s create a simple method which calculates if String’s length is odd or even.

If you call above method with checkOddEvenLenghtString(null), then it will throw NullPointerException as we are calling length method on null object.

Question 7: What is checked exception or compile time exception?

Answer:
Checked exceptions are those exceptions which are checked at compile. If you do not handle them , you will get compilation error.It forces you to handle this exception in try-catch block.

Question 8: Can you put other statements between try,catch and finally block?

Answer:
No, You can not put any statements between try, catch and finally block.

Question 9: How do you create custom exception in java?

Answer:

You just need to extend Exception class to create custom exception.If yo want to create Unchecked Exception, you need to extend RuntimeException.
Let’s understand this with example.You have list of employee and if anyemployee’s age in list of employee is less than 18, then you need to throw invalidAgeException(Our custom exception).
Example: 
Create InvalidAgeException.java as below

Create POJO class called Country.java

Let’s create EmployeeCheckMain.java. This class will have main method.

When you run above program, you will get following output:

As you can see, if we have any employee whose age is less 18,we are throwing InvalidAgeException.

Question 10: Is there any order in which catch block should be written?

Answer:
Yes, most specific exception should be written first and then generic one.
For example:
below code will give you compilation error.

It will give you compilation error with below message. “Unreachable catch block for IOException. It is already handled by the catch block for Exception”.

Question 11:  Difference between throw and throws keyword?

Answer:
Please follow Difference between throw and throws keyword to see the difference.

Question 12 : Predict output of below program:

Output:

Explanation: 
If you notice we have return statement in try block, so before returning from exceptionTest() method, finally block will be executed. When you have return statement in try block, JVM will take note of value of i and this value will be returned by exceptionTest method.

Question 13 : Predict output of below program:

Output:

Explanation:  
Flow of the program will be as below.

  • Value of variable i will be set to 6.
  • NullPointerException will be thrown from try block.
  • Flow will go to catch block and value of i will be set to 10. JVM will make note of value of i and this will be returned by exceptionTest method.
  • Before returning from exceptionTest method, finally block will be executed and “In finally block” will be printed on console.
  • In the end, return value of exceptionTest method will be 10.

Question 14: Predict output of below program:


Output:

Explanation: 
Flow of the program will be as below.

  • Value of variable i will be set to 6.
  • NullPointerException will be thrown from try block.
  • Flow will go to catch block and value of i will be set to 10. We are throwing NullPointerException from catch block.
  • finally will get excuted and value of i will be set to 20.”In finally block” will be printed on console.
  • In the end, return value of exceptionTest method will be 20.
  • If you notice here, return statement from finally block actually suppressed the NullPointerException.

Question 15: Predict output of below program:


Output:

Explanation: 
Flow of the program will be as below.

  • Value of variable i will be set to 6.
  • Value of variable i will be set to 10 and JVM will make note of return value of i as 10.
  • finally will get excuted and value of i will be set to 20.”In finally block” will be printed on console.
  • In the end, return value of exceptionTest method will be 20. It will override value returned by try block.
You may also like:

Add Comment