This post on Java Exception Interview questions will provide you most of the questions that will be asked in an interview. Hope the questions and answers provided in this post will help you in your interviews. You can learn more about Exceptions in my tutorial on Exception Handling in java.
1) What is an Exception ?
An Exception is an abnormal condition that arises in a code sequence at run time. A Java Exception is an object that describes an exceptional condition that has occurred in a piece of code.
When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on.
When an exception occurs in a program, the program execution gets terminated. In such cases we get a system generated error message. In order to avoid termination of the program, the exceptions have to be handled.
2) Why an Exception occurs ?
Exceptions in Java can occur from different kind of situations that can cause a program to throw an exception. For ex. File not found at specified location, Index outside the bounds of an array, Network Connection failure, Database connection issues etc.
3) Explain Java Exception hierarchy ?
In Java Throwable class is at the root of exception hierarchy. All exception types are subclasses of Throwable class. Throwable class is extended by two subclasses Exception and Error.
Exception class is used for exceptional conditions that user programs should catch. This is also the class that
you will subclass to create your own custom exception types. Exceptions are further classified into Checked Exceptions and Runtime Exceptions.
Checked exceptions are those exceptions which are checked at compile-time. It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.
Unchecked exceptions are those exceptions which are checked at run-time and not at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. All Unchecked exceptions are direct sub classes of RuntimeException class.
Error defines exceptions that are not expected to be caught under normal circumstances by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error.
4) What is the difference between Checked and Unchecked exception ?
Checked Exceptions extend Throwable class, but they do not extend RuntimeException or Error classes. UncheckedException extend RuntimeException class.
Checked Exceptions are checked at compile time in Java. Unchecked Exceptions happen at runtime, so they are not checked at compile time.
IOException, ClassNotFoundException, SQLException etc. are examples of CheckedExceptions. NullPointerException, ArithmeticException etc. are examples of Unchecked Exceptions. Checked Exceptions..
5) What is the base class for Error and Exception classes in Java ?
Throwable class is the base class for Error and Exception classes in Java.
6) Exceptions are defined in which java package?
All the exceptions are subclasses of java.lang.Exception.
7) What is the difference between Error and Exception ?
|You can recover from an Exception by using either try-catch blocks or throwing exception back to the caller.||It is not possible to recover from an Error. The only solution to errors is to terminate the execution.|
|Exceptions in java are of type java.lang.Exception.||Errors in java are of type java.lang.Error.|
|Exceptions include both checked as well as unchecked type.||All errors in java are of unchecked type.|
|Checked exceptions are known to compiler where as unchecked exceptions are not known to compiler because they occur at run time.||Errors occur at run time and not known to compiler.|
IndexOutOfBoundsException, ArithmeticException, ClassCastException
AnnotationFormatError, AssertionError, OutOfMemoryError
8) What is Exception Handling in Java ?
Exception handling is a framework that is used to handle runtime exceptions only, compile time exceptions are not handled by exception handling in java. By handling the exceptions we can provide a meaningful message to the user about the issue rather than a system generated message, which may not be understandable to a user.
9) What is the advantage of Exception Handling ?
Exception Handling ensures that the program doesn’t terminate abruptly and execute all the statements in the program without breaking the flow of the program.
10) How will you handle the exceptions in Java ?
Exceptions in java can be handled using try, catch and finally blocks.
11) What is a try block in Java ?
try block in Java is used to enclose the code that might throw an exception. try block must be used within the method and must be followed by either catch or finally block.
12) What is a catch block in Java ?
Catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, Exception Type declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class.
13) What is finally block in Java ?
finally block allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break statements. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. finally block is a block that is used to execute important code such as closing database connection, file streams etc.
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.
14) Can we write only try block without catch and finally blocks ?
We cannot write only try block, it will throw compilation error. The try block must be followed by either catch or finally block.
15) Is there any scenario where finally block will not be executed ?
finally block will not be executed:
1) If program exits either by calling System.exit() or by causing a fatal error that causes the process to abort.
2) If exception is thrown in finally block.