Select Page

-Interview Question and Answer on Exception Handling-

  1. What is error and exception?
    Ans- An exception is a unexpected problem that arises during the execution of a program.The flow of the execution gets disturbed when a exception occurs and the program/Application terminates abnormally. So we need to handle these exceptions. Many reasons can cause exception to occur, given below are some scenarios where exception occurs.
    • A user has entered invalid data.
    • A file that needs to be opened cannot be found.
    An error occurs at compile time. It is not more dangerous than exception.
    E.g.: – A network connection has stop in the middle of communications.
    JVM has run out of memory.
    Stack overflow problem.

A exception can be handled and error cannot be handled.

2) Why java is robust?
Ans- Java is robust because of strong exception handling mechanism and automatic memory management.

3) Hierarchy of exception class.

Java Jitenga

4) Explain checked and unchecked exception with example.

1. Checked exceptions:

Ans- Compile time exceptions are called as checked exceptions because these exception are checked at compile time. This exception occurs due to incorrect inputs or absence of resources. These exceptions are thrown by the complier.

For example:-
• If any table doesn’t exists or the column name is incorrect, compiler throw SQLException which is checked Exception.
• If the filename is incorrect or the file is not specified in the given location, then we will get FileNotFoundException which is again a checked exception.

2. Unchecked exceptions:

An Unchecked exception is an exception that occurs at the time of execution but not checked at compile time, these are also called as Runtime Exceptions. Runtime exceptions are not checked at the time of compilation.

For example:-
• If you try to divide an int by zero, a runtime exception called Arithmetic Exception occurs due to logical problem.

• If the size of the array is 5,and you have inserted more than 5 elements which is logically wrong because size of array is fixed and you cannot insert more than size, so it will throw ArrayIndexOutOfBoundException at runtime.

5) List use of try, catch, throw, throws?
try is used to monitor the code, whatever code we want to inspect that code should be surrounded with try block.
When try implicitly throw any exception, catch block is used to handle the exception so that the program flow continues.
Throw is used to explicitly throw an exception from inside the method. throw can throw only one exception at a time.
throws is used to explicitly throw an exception from outside the method. throws can throw multiple exceptions.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

6) Difference between finally and finalize.

                           Finally Finalize
finally is a block finalize is a method.
finally is executed always whether exception occurs or not finalize is not executed when exception occurs
finally is associated only with try so it clean the resources only within try block. finalize is used to release the resources throughout the class.
finally is called explicitly finalize is called implicitly by gc.

7) Difference between throw and throws.

 Throw Throws
To manually throw the exception  To throw the exception out of the method towards caller when the callee method is not able to handle that exception

public void show() {

throw new NullPointerException();


public void show() {

throw new NullPointerException(“pointing to

null reference”);



public void show()throws IOException,



// body of the method


It is followed by instance of class. It is followed by name of class
It is declared within the method body It is declared with the method definition
It can throw only one exception at a time It can declare multiple exception classes at a time
The exception that is thrown by throw can be handled within that method The exception declared in throws can not be handled by that same method that’s why it is propagated towards caller.


8) What is exception propagation?
Exception Propagation is propagating the exception to the caller .If the caller is not capable of handling the exception then it throws the exception to its caller, this process continues until it being caught by any caller. If no one has caught the exception then it is handled by default exception handler and the program exists.
It can be achieved by throw and throws clause.
Class Test
void m()
int a=10,b=0,c;
void n()
throw new ArithmeticExcpetion();

void p ()
n ();
catch (ArithmeticExcpetion e)
System.out.println (e);
public static void main (String args [])
Test t= new Test ();
t.p ();

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

9) What is user define exception?
If you want to create exception in java, following are the conditions.
• The created exception must be a child of Throwable.
• When creating a checked exception, you need to extend the Exception class.
• When creating a checked exception, you need to extend the RuntimeException class.

class MyException extends Exception{
You just need to give a meaningful name to the exception.

10) What is chained exception?
When one exception is associated with another exception, then such type of exception is called as chained exception. The exception occurs because of another exception ie.one exception is the cause of another exception.

For example:
When we try to divide a number by zero, ArithmeticException occurs .The real reason behind this was that an I/O error occurred, which caused the divisor to be set improperly.

11) Explain multiple catch blocks?
A try block should be followed by multiple catch blocks.
Following is the format for multiple catch block.

try {
//Protected code
catch (ExceptionType1 e1) { //Child ExceptionType
//Handling code
catch(ExceptionType2 e2){ //Child ExceptionType
//Handling code
catch(ExceptionType3 e3){ // Parent Exception Type
//Handling code

Multiple catch blocks are used to customize the exception in different ways. We can specify as many types of exception we want depending on the situation but following the hierarchy rule.

If an exception occurs in try block, then the exception is thrown to the first catch block, if it doesn’t get match then it throws to the second catch block, this continues until it gets a matching catch. If there is no matching catch, exception will be handled by default exception handler.

12) List exception methods?

1 public String getMessage ():-

It will display a detailed message about the exception that has occurred.

2 public Throwable getCause ():-
returns the reason of the exception thrown by a Throwable object.

3. public void printStackTrace ():-

Returns the package name, class name, method name and line no of the exception occurs.
i.e. it traces the exception from the root.

4. public StackTraceElement [] getStackTrace ():-

Returns an array which consists of each element on the stack trace. index 0 contains
the top of the call stack, and the last element in the array contains the method
at the bottom of the call stack.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

13) List the situation when finally will not get executed?

The situations are as below:

1) If we write System. exit () in try block, then the program will be terminated finally block will not get executed.
2) If a compilation error occurs or when the loop goes into infinity.
3) If there is a exception in a code which is not surrounded by try, finally will not get executed as finally is associated only with try.

14) What happens when exception is thrown by main method?
When exception is thrown by main () method, Java Runtime terminates the program and print the exception message and stack trace in system console.

15) Can we write only try block without catch and finally blocks?
No we cannot write only try block as it will give compilation error, try block should either followed by catch or finally block.

16) What is Re-throwing an exception in java?
Whenever an exception arises in the try block they are handled in the catch block. But they can be a situation where catch block is unable to handle that exception; it can re-throw that exception using throw keyword. This process is called re-throwing an exception.
String s = null;
System.out.println (s.length ()); //This statement throws NullPointerException
catch (NullPointerException ex)
System.out.println (“NullPointerException is caught here”);
throw ex; //Re-throwing NullPointerException

17) Why it is always recommended that clean up operations like closing the DB resources to keep inside a finally block?
As finally block always get executed whether exceptions occurs in the try block. If we write cleanup operations in finally block, we will be ensure that those operations will always be executed irrespective of whether exception is occurred or not.

18) Can we override a super class method which is throwing an unchecked exception with checked exception in the sub class?
No, according to the rule of method overriding if a super class method is throwing an unchecked exception, then it can be overridden in the sub class with same exception or unchecked exceptions but cannot throw checked exceptions.

Pallavi Kadam Software Trainer
SevenMentor Pvt Ltd.


Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

Pin It on Pinterest

× How can I help you?