0% found this document useful (0 votes)
4 views3 pages

Java Exceptions Interview Questions

Uploaded by

Prashant Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Exceptions Interview Questions

Uploaded by

Prashant Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Exceptions and Exception Handling - Interview Questions

1. What is an exception in Java?


An exception is an event that occurs during the execution of a program that disrupts the normal flow of
instructions.
When an error happens, Java creates an exception object and hands it over to the runtime system. If not
handled, the program will terminate abnormally.

2. What is the difference between checked and unchecked exceptions in Java?


- Checked exceptions are exceptions that the compiler forces you to handle using try-catch or by declaring
them with throws. Examples: IOException, SQLException.
- Unchecked exceptions are runtime exceptions that are not checked at compile-time. Examples:
NullPointerException, ArrayIndexOutOfBoundsException.

3. What is the purpose of the try, catch, and finally blocks in Java?
- try: Defines the block of code where an exception might occur.
- catch: Catches and handles the exception.
- finally: Executes code regardless of whether an exception occurs or not.
Example: A finally block is useful for closing resources like files or database connections.

4. What happens if an exception is not caught in a program?


If an exception is not handled, Java's default exception handler terminates the program and prints the stack
trace.

5. What is the difference between throw and throws in Java?


- throw: Used to explicitly throw an exception in code.
- throws: Declares exceptions that a method might throw, so the caller is aware.
Example: throw new ArithmeticException("Cannot divide by zero");

6. Can a finally block exist without a catch block?


Yes, a finally block can be used with a try block alone. The finally block will execute no matter what,
ensuring resource cleanup.

7. What are some common runtime exceptions in Java?


- NullPointerException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ClassCastException
- IllegalArgumentException

8. What is the throw keyword used for in Java?


The throw keyword is used to manually throw an exception inside a method.

9. What are custom exceptions in Java?


A user-defined exception class that extends Exception or RuntimeException. Used when built-in exceptions
are not sufficient.

10. What is the difference between Exception and Error in Java?


- Exception: Recoverable (e.g., IOException).
- Error: Irrecoverable (e.g., OutOfMemoryError).

11. How does exception propagation work in Java?


If an exception is not caught in a method, it propagates up the call stack to the caller method.

12. What is the purpose of the throws keyword in method signatures?


Used to inform that a method might throw exceptions, forcing the caller to handle them.

13. Can we have multiple catch blocks in Java?


Yes, multiple catch blocks can be used to handle different types of exceptions separately.

14. What is multi-catch in Java?


Introduced in Java 7, multi-catch allows handling multiple exceptions in a single catch block:
catch (IOException | SQLException ex) { ... }

15. What is the try-with-resources statement in Java?


Introduced in Java 7, it automatically closes resources like files after execution.

16. How can we handle multiple exceptions in Java efficiently?


Using multi-catch (|) or a single catch block for the base class Exception.

17. What is stack trace in exception handling?


A report that shows the method call hierarchy when an exception occurs.

18. Can we create custom checked and unchecked exceptions?


Yes, by extending Exception (checked) or RuntimeException (unchecked).

19. What is rethrowing an exception?


Catching an exception and throwing it again to be handled further up.

20. What are suppressed exceptions in Java?


Exceptions that occur but are suppressed in favor of another exception.

21. What is the difference between final, finally, and finalize in Java?
- final: Keyword for constants, methods, or classes.
- finally: A block ensuring cleanup.
- finalize(): A method in Object for garbage collection (deprecated).

22. What happens when an exception is thrown in a static block?


The class will fail to load, resulting in an ExceptionInInitializerError.

23. Can a constructor throw an exception in Java?


Yes, but it must be handled when creating the object.

24. What are chained exceptions in Java?


Wrapping one exception inside another for better debugging.

25. What is exception wrapping?


Encapsulating a low-level exception into a higher-level exception to provide more context.

These questions cover a wide range of topics related to exceptions and exception handling in Java. Let me
know if you need more details!

You might also like