Exception Handling Interview Questions
Exception Handling Interview Questions
1) What is an exception?
Exceptions in Java are handled using try, catch and finally blocks.
try block : The code or set of statements which are to be monitored for exception
are kept in this block.
catch block : This block catches the exceptions occurring in the try block.
finally block : This block is always executed whether an exception occurs in the try
block or not and an exception is caught in the catch block or not.
4) Can we keep other statements in between try, catch and finally blocks?
No. We shouldn’t write any other statements in between try, catch and finally
blocks. They form one unit.
try
{
// Statements to be monitored for exceptions
}
catch(Exception ex)
{
//Catching the exceptions here
}
finally
{
// This block is always executed
}
5) Can we write only try block without catch and finally blocks?
No, It shows a compilation error. The try block must be followed by either catch or
finally block. You can remove either catch block or finally block but not both.
No. Once a try block throws an exception, remaining statements will not be executed.
control comes directly to the catch block.
When you are keeping multiple catch blocks, the order of catch blocks must be from
most specific to most general ones. i.e subclasses of Exception must come first and
super classes later. If you keep super classes first and sub classes later, the compiler
will show an unreachable catch block error.
public class ExceptionHandling
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt("abc"); //This statement
throws NumberFormatException
}
catch(Exception ex)
{
System.out.println("This block handles all exception
types");
}
catch(NumberFormatException ex)
{
//Compile time error
//This block becomes unreachable as
//exception is already caught by above catch block
}
}
}
The exceptions which occur at run time are called runtime exceptions. These
exceptions are unknown to compiler. All subclasses of
java.lang.RunTimeException and java.lang.Error are runtime exceptions.
These exceptions are unchecked type of exceptions. For example,
NumberFormatException, NullPointerException, ClassCastException,
ArrayIndexOutOfBoundException, StackOverflowError etc.
10) What is OutOfMemoryError in Java?
Checked exceptions are the exceptions which are known to the compiler. These
exceptions are checked at compile time only. Hence the name checked exceptions.
These exceptions are also called compile time exceptions. Because, these exceptions
will be known during compile time.
Unchecked exceptions are those exceptions which are not at all known to the compiler.
These exceptions occur only at run time. These exceptions are also called run time
exceptions. All subclasses of java.lang.RunTimeException and
java.lang.Error are unchecked exceptions.
13) Can we keep the statements after the finally block If the control is returning
from the finally block itself?
No, it gives an unreachable code error. Because, control is returning from the finally
block itself. Compiler will not see the statements after it. That’s why it shows
unreachable code errors.
14) Does the finally block get executed If either try or catch blocks are returning
the control?
Yes, the finally block will be always executed no matter whether try or catch blocks are
returning the control or not.
15) Can we throw an exception manually? If yes, how? Yes, we can throw an
exception manually using throw keyword. Syntax for throwing an exception manually is
throw InstanceOfThrowableType;
Below example shows how to use throw keyword to throw an exception manually.
try
{
NumberFormatException ex = new NumberFormatException();
//Creating an object to NumberFormatException explicitly
try
{
String s = null;
System.out.println(s.length()); //This statement throws
NullPointerException
}
catch(NullPointerException ex)
{
System.out.println("NullPointerException is caught here");
18) Why it is always recommended that clean up operations like closing the DB
resources to keep inside a finally block?
Because finally block is always executed whether exceptions are raised in the try block
or not and raised exceptions are caught in the catch block or not. By keeping the clean
up operations in finally block, you will ensure that those operations will be always
executed irrespective of whether exception is occurred or not.
19) What is the difference between final, finally and finalize in Java?
Click here to see the differences between final, finally and finalize in Java.
22) What is the difference between throw, throws and throwable in Java?
Click here to see the differences between throw, throws and throwable in Java.
27) What are the legal combinations of try, catch and finally blocks?
1) try
{
//try block
}
catch(Exception ex)
{
//catch block
}
2)
try
{
//try block
}
finally
{
//finally block
}
3)try
{
//try block
}
catch(Exception ex)
{
//catch block
}
finally
{
//finally block
}
31) Do you know try-with-resources blocks? Why do we use them? When they are
introduced?
Try-with-resources blocks are introduced from Java 7 to auto-close the resources like
File I/O streams, Database connection, network connection etc… used in the try block.
You need not to close the resources explicitly in your code. Try-with-resources implicitly
closes all the resources used in the try block.