Java Exception Handling Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. Explain the difference between checked and unchecked exceptions.
Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.
Ques 2. How can you handle multiple exceptions in Java?
You can use multiple catch blocks or a catch block with multiple exception types separated by '|'.
Example:
try { /* code that may throw exceptions */ } catch (ExceptionType1 | ExceptionType2 e) { /* handle exception */ }
Ques 3. Explain the concept of 'try-with-resources' in Java.
'try-with-resources' is a feature introduced in Java 7 to automatically close resources like files or sockets when they are no longer needed.
Example:
try (FileReader fr = new FileReader("file.txt")) { /* code that uses fr */ }
Ques 4. What is the difference between 'Error' and 'Exception'?
'Error' is thrown by the JVM and is not meant to be caught or handled, while 'Exception' is meant to be caught and handled.
Ques 5. What is the purpose of the 'throws' clause in method signature?
The 'throws' clause is used to declare that a method may throw one or more types of exceptions. It is part of the method signature.
Example:
void myMethod() throws CustomException { /* method code */ }
Ques 6. Explain the difference between 'throw' and 'throws' in Java.
'throw' is used to explicitly throw an exception, while 'throws' is used in method signature to declare that the method may throw one or more types of exceptions.
Ques 7. How can you create a custom exception in Java?
To create a custom exception, you need to extend the 'Exception' class or one of its subclasses.
Example:
class CustomException extends Exception { /* constructor and additional code */ }
Ques 8. Can you have multiple 'finally' blocks in a try-catch-finally statement?
No, there can be at most one 'finally' block associated with a 'try-catch' statement.
Ques 9. What is the difference between 'System.exit(0)' and throwing an exception?
'System.exit(0)' terminates the Java Virtual Machine, while throwing an exception allows for a controlled termination of a specific part of the program.
Ques 10. Explain the 'try-with-resources' syntax introduced in Java 7.
'try-with-resources' simplifies resource management by automatically closing resources that are declared in the try statement. Resources must implement the 'AutoCloseable' interface.
Example:
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { /* code using br */ }
Ques 11. Can you rethrow an exception in Java?
Yes, you can rethrow an exception by using the 'throw' statement without an argument inside a catch block.
Example:
catch (Exception e) { /* handling code */ throw e; }
Ques 12. Explain the difference between 'Error' and 'RuntimeException'.
'Error' is meant to indicate serious problems that should not be caught or handled, while 'RuntimeException' is a subclass of 'Exception' and represents exceptions that can be caught and handled.
Ques 13. What is the 'NullPointerException' and how can you prevent it?
'NullPointerException' occurs when you try to access or modify an object reference that is null. You can prevent it by checking for null before accessing the object.
Example:
if (obj != null) { /* code using obj */ }
Ques 14. What is the purpose of the 'Assertion' in Java?
Assertions are used to test assumptions about the program. They help in identifying logical errors during development and testing.
Example:
assert x > 0 : "x should be greater than 0";
Ques 15. How can you handle exceptions in a servlet?
In servlets, exceptions can be handled using the 'try-catch' block or by declaring them in the 'throws' clause of the method.
Most helpful rated by users: