» » » Top 20 Java Exception handling interview questions and answers

1.What is an exception?


  • Exceptions are the objects representing the logical errors that occur at run time and makes JVM enters into the state of  "ambiguity".
  • The objects which are automatically created by the JVM for representing these run time errors are known as Exceptions



2.What are the differences between exception and error.

  • An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
  • Difference between exception and error

3.How the exceptions are handled in java

  • Exceptions can  be handled using try catch blocks.
  • The statements which are proven to generate exceptions need to keep in try block so that whenever is there any exception identified in try block that will be assigned to corresponding exception class object in catch block
  • More information about try catch finally

4.What is the super class for Exception and Error

  • Throwable.


  1. public class Exception extends Throwable implements Serializable


  1. public class Error extends Throwable implements Serializable

5.Exceptions are defined in which java package

  • java.lang.Exception

6.What is throw keyword in java?

  • Throw keyword is used to throw exception manually.
  • Whenever it is required to suspend the execution of the functionality based on the user defined logical error condition we will use this throw keyword to throw exception.
  • Throw keyword in java

7.Can we have try block without catch block

  • It is possible to have try block without catch block by using finally block
  • Java supports try with finally block
  • As we know finally block will always executes even there is an exception occurred in try block, Except System.exit() it will executes always.
  • Is it possible to write try block without catch block

8.Can we write multiple catch blocks under single try block?


exception handling interview questions and answers


9. What are unreachable blocks in java

  • The block of statements to which the control would never reach under any case can be called as unreachable blocks.
  • Unreachable blocks are not supported by java.
  • Unreachable blocks in java


unreachable block exception handling interview question



10.How to write user defined exception or custom exception in java



  1. class CustomException extends Exception { } // this will create Checked Exception
  2. class CustomException extends IOExcpetion { } // this will create Checked exception
  3. class CustomException extends NullPonterExcpetion { } // this will create UnChecked
  4. exception

11.What are the different ways to print exception message on console.

  • In Java there are three ways to find the details of the exception .
  • They are 
  1. Using an object of java.lang.Exception
  2. Using public void printStackTrace() method
  3. Using public String getMessage() method.
print exception message in java



12.What are the differences between final finally and finalize in java




13.Can we write return statement in try and catch blocks


return statement in try catch block exception handling



14. Can we write return statement in finally block



return statement in finally exception handling interview


15. What are the differences between throw and throws


  •  throw keyword used to throw user defined exceptions.(we can throw predefined exception too)
  • Using throws keyword we can explicitly provide the information about unhand-led exceptions of the method to the end user, Java compiler, JVM.
  • Throw vs throws

throw vs throws

 16.Can we change an exception of a method with throws clause from unchecked to checked while overriding it? 


  •  No. As mentioned above already
  • If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.
  • So we can not change from unchecked to checked 

 17.What are the rules we need to follow in overriding if super class method throws exception ?


  •  If sub class throws checked exception super class should throw same or super class exception of this.
  • If super class method  throws checked or unchecked exceptions its not mandatory to put throws in sub class overridden method.
  • If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.

 

18.What happens if we not follow above rules if super class method throws some exception.

  •  Compile time error will come.

  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws Exception{ //Compiler Error: Exception Exception is not compatible with
  5. throws clause in Super.add()
  6. System.out.println("Sub class add method");

  7. }

  8. }

  1. package ExceptionHandlingInterviewPrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package ExceptionHandlingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws NullPointerException{ // this method throws unchecked exception so no
  5. isuues
  6. System.out.println("Sub class add method");

  7. }

  8. }

 19. Is it possible to write multiple exceptions in single catch block


  • It is not possible prior to java 7.
  • new feature added in java 7.


  1. package exceptionsFreshersandExperienced;
  2. public class ExceptionhandlingInterviewQuestions{
  3.  
  4. /**
  5.  * @www.instanceofjava.com
  6.  **/
  7.  public static void main(String[] args) {

  8.  
  9. try {
  10.  
  11. // your code here
  12.             
  13. } catch (IOException | SQLException ex) {
  14.             System.out.println(e);
  15. }
  16.  
  17. }
  18.  
  19. }



20. What is try with resource block

  • One more interesting feature of Java 7 is try with resource 
  • In Java, if you use a resource like input or output streams or any database connection logic you always need to close it after using. 
  • It also can throw exceptions so it has to be in a try block and catch block. The closing logic  should be in finally block. This is a least the way until Java 7. This has several disadvantages:

    1.     You'd have to check if your ressource is null before closing it
    2.     The closing itself can throw exceptions so your finally had to contain another try -catch
    3.     Programmers tend to forget to close their resources

  • The solution given by using try with resource statement.

  1. try(resource-specification) 
  2. {
  3.  
  4. //use the resource
  5.  
  6. }catch(Exception e)
  7. {
  8.  
  9. //code

  10. }

  • Top 20 Java Exception handling interview questions and answers for freshers and experienced

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu