Java Exception Handling Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is an exception in Java?
An exception is an event that disrupts the normal flow of the program's instructions during runtime.
Example:
int result = 10 / 0; // This will throw an ArithmeticException
Ques 2. What is the purpose of the 'finally' block in exception handling?
The 'finally' block is used to execute important code such as closing resources, whether an exception is thrown or not.
Ques 3. What is the purpose of the 'throw' keyword?
The 'throw' keyword is used to explicitly throw an exception.
Example:
throw new CustomException("This is a custom exception");
Ques 4. Can you have a try block without a catch block?
Yes, you can have a try block without a catch block if there is a finally block.
Example:
try { /* code */ } finally { /* cleanup code */ }
Ques 5. What is the purpose of the 'NullPointerException' in Java?
The 'NullPointerException' is thrown when attempting to access or modify an object reference that is null.
Example:
String str = null;
int length = str.length(); // This will throw NullPointerException
Ques 6. What is the purpose of the 'try' block?
The 'try' block is used to enclose a block of code where exceptions might occur.
Example:
try { /* code that may throw exceptions */ } catch (Exception e) { /* handle exception */ }
Ques 7. What is the purpose of the 'RuntimeException' class?
'RuntimeException' is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
Ques 8. What is the purpose of the 'AssertionError' class?
'AssertionError' is thrown to indicate that an assertion has failed. It extends 'Error' class.
Ques 9. What is the purpose of the 'Error' class in Java?
'Error' class represents serious errors that are not meant to be caught or handled by applications.
Ques 10. How do you handle custom exceptions in a try-catch block?
You can catch custom exceptions like any other exception by specifying the custom exception type in the catch block.
Example:
try { /* code */ } catch (CustomException ce) { /* handle custom exception */ }
Ques 11. What is the purpose of the 'FileNotFoundException' in Java?
'FileNotFoundException' is thrown when an attempt to open the file denoted by a specified pathname has failed.
Example:
try { FileReader fr = new FileReader("nonexistent.txt"); }
catch (FileNotFoundException e) { /* handle file not found exception */ }
Intermediate / 1 to 5 years experienced level questions & answers
Ques 12. Explain the difference between checked and unchecked exceptions.
Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.
Ques 13. 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 14. 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 15. 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 16. 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 17. 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 18. 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 19. 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 20. 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 21. 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 22. 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 23. 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 24. 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 25. 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 26. 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.
Experienced / Expert level questions & answers
Ques 27. What is the difference between 'finally' block and 'final' keyword?
'finally' is used in exception handling to define a block of code that will be executed whether an exception is thrown or not. 'final' is a keyword used to apply restrictions on class, method, and variable.
Ques 28. Explain the concept of 'suppressed exceptions' in Java.
Suppressed exceptions are exceptions that are not propagated up the call stack and are associated with try-with-resources. They are added to the primary exception as suppressed exceptions.
Ques 29. How can you handle exceptions in a multi-threaded environment?
You can use try-catch blocks inside the run() method of a thread or implement the 'Thread.UncaughtExceptionHandler' interface.
Ques 30. Explain the 'try', 'catch', 'finally' and 'throw' keywords in Java.
'try' is used to enclose a block of code where exceptions might occur. 'catch' is used to handle exceptions. 'finally' is used to define a block of code that will be executed whether an exception is thrown or not. 'throw' is used to explicitly throw an exception.
Most helpful rated by users: