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:

Related Posts

  • 22 May

    Java interview questions

    I have written a lot of java interview questions and this is index posts for all java interview questions. I will keep adding more interview questions to this post. Core Java Core java interview questions This list includes top 50 core java interview questions. Whether you are fresher or experienced programmer, this interview questions will […]

  • 09 November

    SOLID Principles in Java

    In this post, we will see 5 SOLID Principles in Java. Robert C. Martin gave five objected oriented design principles, and the acronym S.O.L.I.D is used for it. Each letter of this acronym talks about principles in Java. When you use all the principles of S.O.L.I.D in a combined manner, it becomes easier for you […]

  • 08 November

    Java interview questions for 2 years experience

    In this post, we are going to see Java interview questions for 2 to 3 years experienced. These are the most asked interview questions for freshers(0-3 years experienced). This question list will help you to crack java interview. I have already shared detailed answer over here before, you might find it helpful as well. I […]

  • 08 November

    Java interview questions for 5 years experience

    In this post, we will see Java interview questions for 5 to 6 years experience. When you have 5 years of experience as java developer, you need to have a good understanding of collections, multithreading concepts. If you are looking for the below queries then this post will help you as well. Java interview questions […]

  • 11 September

    Java technical interview questions

    In this post, we will see top Java technical interview asked in the Core Java interview.I have covered a lot of interview question on Java programming. Here is list of other java interview question you might find useful. Core java interview questions Java Collections interview questions Java String interview questions OOPs interview questions in java […]

  • 06 August

    Top 15 Spring Boot Interview questions

    In this post, we will see top 15 Spring Boot interview questions with answers.If you want to read more about Spring boot, you can go through Spring boot tutorial. If you are looking for below queries then this post will help you as well. Spring boot interview questions for 3 years experience Spring boot interview […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.