Exception Handling Interview Questions for Intermediate
11. Write the difference between throw, throws, and throwable in Java.
Throw keyword in Java is used to throw an exception manually. Exception should be of type java.lang.Throwable class or its subclasses
class ThrowExample{
void method() throws Exception
{
Exception ex = new Exception();
throw ex;
}
}
throws keyword in java is used in the method declaration to indicate that this method may throw exceptions.
Let's take a look at the example below:
class ThrowsExample
{
void methodOne() throws SQLException
{
//This method may throw SQLException
}
}
Throwable forms a part of the super class that is for all types of errors and exceptions in java. Let's take a look at the following example.
class MyException extends Throwable
{
//Customized Exception class
}
12. Write the difference between final, finally, and finalize in Java.
Final is an access modifier. finally, a block in Exception Handling and finalize forms a method of the object class. Moreover, the final method is called when we execute it, finally executed after the try-catch block is executed, and finalize executes just before the object is destroyed.
13. What happens if we have statements after the finally block and the control is returning from the finally block itself?
This gives an unreachable code error. As the control is returning from the finally block itself, the compiler will not look into the statements after it. Hence, it shows an unreachable code error.
14. What do you mean by ClassCastException?
It is a RuntimeException that occurs when JVM is unable to cast an object of one type to another type.
15. Do you know try-with-resources blocks? Why do we use them? When are they introduced?
The try statement that declares one or more resources and ensures that each of them is closed finally at the end of statement execution is called the try-with-resources block.
16. Can you have an empty catch block?
A catch block is used to catch the exceptions found in the try block. Actually, we can have an empty catch block, but there are better practices to implement.
17. What does JVM do when an exception occurs in a program?
An exception Object is created, which is thrown to inform that an error has occurred. If the Exception is not handled correctly, JVM displays an error message and terminates.
18. Is it possible to have a return statement in the catch or finally block?
Yes, we can have a return statement in the catch or finally block; if we write a return statement at the end, the code will execute successfully, and the code below it will become a part of unreachable code.
19. What is re-throwing an exception?
When an exception is found in the catch block, first of all, the Exception is caught by the catch block. Then, the Exception has to be re-thrown. To re-throw an exception, we use the throw keyword.
20. What is Exception Propagation?
Exception propagation is another concept of Java Exception handling. It refers to the transferring of Exceptions from one method to the previous method in order to handle the Exception efficiently.
Exception Handling Interview Questions for Experienced
21. Explain User Defined Exceptions. Give an example.
User Defined Exceptions are the exceptions created by the programmers to use their own designed exceptions and those not present in the java exception library.
Java
class ninjas{
public static void main(String args[]){
try{
int a = 7;
int b = 10;
int f = a / b;
System.out.println(f);
if(f == 0){
throw new MyException1("Designed Exception");
}
}
catch(MyException1 e){
System.out.println(e.getMessage());
}
}
}
class MyException1 extends Exception {
public MyException1(String message){
super(message);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
0
Designed Exception
22. Explain an unreachable catch block error.
As Exception is the superclass of all exceptions, the catch block provided with relation to it should and must always come last. When retaining numerous catch blocks, the catch blocks must be kept in ascending order of specificity. Exception subclasses should always come first, followed by superclasses. The compiler will generate an error if we continue to order superclasses before subclasses.
23. Explain the hierarchy of exceptions in Java.
The hierarchy of exceptions in Java is shown as follows:
- java.lang.Error: It is a super class for all types of errors in Java.
- java.lang.VirtualMachineError: –
- StackOverFlowError
- OutOfMemoryError
- java.lang.AssertionError
- java.lang.LinkageError:
- NoClassDefFoundError
- IncompatibleClassChangeError
- java.lang.Exception: It is a super class of all exception types in Java. Common
- RunTimeException
- ArithmeticException
- NumberFormatException
- NullPointerException
- ArrayIndexOutOfBoundsException
- ClassCastException
- java.lang.InterruptedException
- java.lang.IOException
- java.lang.SQLException
- java.lang.ParseException
24. Can we throw multiple exceptions in one throw statement?
Ans: No, we cannot throw multiple exceptions in one throw statement. Only one object of exception type can be thrown by using a throw statement at a time.
25. What is a chained exception in java?
Throwing an exception along with another exception is called a chained exception in java.
26. What are the methods provided by the Throwable class in Java?
The methods provided by the Throwable class are as follows:
- getMessage()
- toString()
- printStackTrace()
- fillInStackTrace()
- getStackTrace()
- getClause()
27. Explain stack trace in Java. Why is it important in exception handling?
A stack trace contains information regarding methods called during your execution of the application. A stack trace is a debugging tool as it helps in the determination of the exact point where the exception occurred in the application and also the reason why it occurred.
28. Mention a few of the best practices in Java exception handling.
- Use a finally block to clean up resources or close them.
- Throw specific exceptions.
- Do not catch the Exception class. Catch the specific sub classes.
- No exception must be thrown from the finally block.
- Use descriptive messages when throwing exceptions.
29. Is it possible to override a superclass method that is throwing an unchecked exception along with a checked exception in the sub class?
When a super class method is throwing an unchecked exception, then it is possible for it to be overridden in the sub class with the same exception or any other unchecked exceptions, but it's not possible to do so with checked exceptions.
30. It is always recommended to close the DB resources to keep them inside a finally block. Why?
The finally block is the block that is always executed no matter whether the exceptions are raised or not or caught. Cleanup Operations ensure that these will always be executed irrespective of whether an exception occurs or not.
Frequently Asked Questions
What are the different scenarios causing exceptions in Thread Main?
Exceptions in the main thread can occur due to:
- Unhandled runtime errors in the main() method (e.g., NullPointerException, ArithmeticException).
- Synchronous code failures.
- Explicitly throwing exceptions without proper handling in the main thread.
What are the 5 keyword exception handling in Java?
In Java, the key exception-handling keywords are: try(used to enclose a block of code), catch(used to catch and handle exceptions), finally(used to define a block of code that will be executed regardless of whether an exception occurred or not), throw(used to explicitly throw an exception), and throws(used in method signatures to indicate that the method might throw specific exceptions, which need to be handled by the caller or propagated further).
How do you handle exceptions interview questions?
Exception handling involves using try to enclose risky code, catch to manage exceptions, finally for cleanup, throw to create exceptions, and throws to indicate potential exceptions in methods. It ensures smoother program execution and error recovery.
Conclusion
We discussed a comprehensive list of 30 Java Exception Handling Interview Questions covering essential concepts, from basics to advanced topics. We hope these Java Exception Handling Interview Questions helped you enhance your knowledge. Exception forms an important topic of Java and hence it’s important to study Exception handling. Refer to the above Java Exception Handling Interview Questions as the collection of the most asked questions in any Java interview regarding Exception Handling.
If you found this blog interesting and insightful, refer to similar blogs: