Java Exception Handling Interview Questions And Answers

Exception Handling Interview Questions
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

  • Hibernate interview questions
    16 April

    Top 40+ Hibernate interview questions and answers

    Table of Contents1. What is ORM?2. What is Hibernate?3. What are advantages of Hibernate?4. Explain architecture of Hibernate?5. What are some core interfaces of hibernate?6. Explain brief about Session interface used in hibernate?7. Are session objects thread safe?8. Explain brief about SessionFactory object used in hibernate?9. What is Configuration class in hibernate?10. Explain brief about […]

  • Java Interview questions
    22 May

    Java interview questions

    Table of ContentsCore JavaCore java interview questionsCollections interview questionsMultithreading interview questionsJava 8 interview questionsOOPs interview questionsException Handling interview questionsString interview questionsSerialization interview questionsmethod overloading and method overriding interview questionsImmutable class interview questionsJava tricky interview questions Basic interview questions for fresher Interview questions for 5 to 7 years experiencedHow HashMap works in java? Difference between Interface […]

  • Solid principles in java
    09 November

    SOLID Principles in Java

    Table of ContentsWhat is the meaning of S.O.L.I.D?Single Responsibility Principle (SRP)Open Closed Principle Liskov Substitution Principle Interface Segregation PrincipleDependency Inversion PrincipleWrap Up 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 […]

  • Java interview questions for 2 years experience
    08 November

    Java interview questions for 2 years experience

    Table of Contents1.   Can we override static method in java?2.   Can you overload main method in java?3. Can we override private methods in java?4. What is the base class for all the classes?5.   Can you list down some of important method from object class?6.   Which two methods should you override while putting the custom object as […]

  • Java interview questions for 5 years experience
    08 November

    Java interview questions for 5 years experience

    Table of Contents1. Guess the output of below program.2. Guess the output of below program.3. Guess the output of below program.4. What will happen in case of below program?5. What will happen in case of below program?6. What will happen in case of below program?7. Guess the output of below program.8. Guess the output of […]

  • Java tricky interview questions
    11 September

    Java technical interview questions

    Table of Contents1 How HashMap works in java?2 Which methods you should implement for key Object in HashMap?3. Guess the output of below program.4. Guess the output of below code.5. What are differences between Comparator and Comparable?6. What is difference between Error and Exception?7. What is ClassLoader in java?8. What are types of ClassLoader in java?9. […]

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.