+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

Java Exception Interview Questions and Answers


1
What is Exception Handling?

Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.

2
What is an Exception? Explain by giving an example.

Exceptions are errors that occur at runtime and disrupt the normal flow of execution of instructions in a program.
An exception object is created by the method in which an error occurs which is then handed over to the runtime system. This process is called throwing an exception. The object contains the details of the error, its type, etc.
An exception handler is the code that executes after an exception is encountered. The runtime method searches the call stack to find an appropriate method containing the code for handling the exception. The runtime system after receiving the exception tries to find a suitable way to handle it.
When the type of exception thrown matches the type of exception that can be handled, the exception is passed on to the handler so as to catch the exception. The three types of exceptions are:
checked exceptions, errors and runtime exceptions.
An event that occurs during the execution of a program that disrupts the normal flow of instructions is called an exception.
Eg:
public static void Main ()
{
int numerator, denominator;
try
{
int quotient = numerator/denominator;
}
catch (DivideByZeroException e)
{
System.out.println("Divide By Zero Exception Occurred!");
}
}

3
What is exception propagation ?

Forwarding the exception object to the invoking method is known as exception propagation.
An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method,If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack.This is called exception propagation.

4
What is the purpose of the finally clause of a try-catch-finally statement?

The finally block is the region where the code that needs to be executed under any circumstance is written. The purpose of this block is usually performing the clean up activities.
Eg: freeing the resources, etc. If the finally clause executes a return statement, it overrides a thrown exception (so the exception will not be thrown; instead the return will occur).

5
What is difference between Checked Exception and Unchecked Exception?
  1. Checked Exception
    The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.
  2. Unchecked Exception
    The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.
Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US