Mostly Asked Java Exception Handling Interview Questions And Answers.


1.What is an exception?

  • An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.


2.Which class act as a root class for Exception?

  • Throwable


3.What is checked and unchecked exception?

  • The exceptions which are checked by the compiler at run time is called checked exception.

EX:IOException

uncheckedException:

  • The Exceptions which are not checked by compiler are called unchecked Exceptions.

EX:NullPointerException.


4.How to handle Exceptions in java?

We can handle the exceptions in java two ways.

  • by using try,catch blocks

  • by using throws

try:

we need to place the code that might throw an exception within a try block. a try block looks like the following


try{
code
}catch and finally

catch:

The catch block contains code that is executed if and when the exception handler is invoked. That means catch block catches the exceptions which are raised try block.

throws:

The main purpose of throw keyword is to delegate the responsibility of exception handling to the caller method in the case of checked exception


5.What is finally Block?

The finally block always executes when the try block exits. This ensures that the finally block is executed whether exception is raised or not.

We can place the clean-up code with in finally block which should be executed always.

There is only situation finally block should not executed i.e. when we call with in the try or catch bloc k system.exit(O).


6.What will happen if we write any return statement with in the try block?

If there is any return statement in try block then first finally will be executed.then return statement is considered.


7.What is the difference between final,finally& finalize?

final is a modifier applicable for classes,methods and variables.

If a class declared as final then we cant create child class.

If a method declared as final then we are not allowed to override that method in child classes.

If a variable declared as final then we cant change the value of a variable.

finally is a block which is always associated with try-catch block to maintain the cleanup code which should be executed always whether exception raised or not.

finalize()is a method which should be executed by garbage collector before destroying any object.


8.Some possible combinations of try-catch-finally?


Try{
}catch(exception e){
}
Try{
}catch(ArithmaticException e){
}catch(Exception e){
}

Try{
}finally{
}


9.How do you create custom exception in java?

By using throw keyword we can create customized exception.


package com.practice;

public class VoteException extends Exception {

public VoteException(String message) {
super(message);
}



}

package com.practice;

public class Test {
public void checkAge(int age)throws VoteException{
if(age>18)
throw new VoteException("eligible for vote");
else
System.out.println("you are not eligible for vote");
}
public static void main(String[] args) {
Test t = new Test();
try{
t.checkAge(15);
}catch(Exception e){
e.printStackTrace();
}
}

}

output:


you are not eligible for vote


10.What is StackoverFlowError?

It is the child class of error and hence it Is unchecked. This will be raised automatically by the JVM,whenever we are trying to perform recursive method invocation.

The code will be like Following:


public class Test{
public void method1(){
method2();
}
public void method2(){
method1();
}
}

11.Give me some examples of checked exceptions and unchecked Exceptions?

Checed Exceptions:

  • IOException
  • Servle Exception
  • InterruptedException
  • SQLException

Unchecked Exceptions:

  • Stackoverflow Error
  • OUTOFMemory Error
  • ArithmaticExceptions


12.What is the difference between the ClassNotFound and NoClassDefFoundError?

  • public class ClassNotFoundException extends Exception will be thrown when an application tries to load in a class through its string name using.

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

NoClassDefFoundError will be thrown by jvm automatically when jvm unable to find required classes.

For example if we class LiKe person then if we try to call at run time like java person. if person.class file is not available then exception will be thrown saying NoClassDefFoundError.

No comments:

Post a Comment