Loading
Java - Exception hierarchy-interview

Q1. Explain the Exception Hierarchy in Java.

At the top is the Throwable class, which has two main subclasses:

  • Exception Represents conditions that programs should handle.
  • Error Represents serious problems that applications should not handle (e.g., OutOfMemoryError).

Exception further splits into:

  • Checked exceptions (compile-time) – e.g., IOException, SQLException.
  • Unchecked exceptions (runtime) – e.g., NullPointerException, ArithmeticException.





Q2. What is the difference between Error and Exception in Java?
  • Error: Irrecoverable conditions (e.g., JVM crash, OutOfMemoryError).
  • Exception: Conditions programs can catch and handle.





Q3. What is the difference between checked and unchecked exceptions?
  • Checked exceptions: Must be declared or handled in code. Compiler enforces it. Example: IOException.
  • Unchecked exceptions: Not required to be declared or handled. Example: NullPointerException.




Q4. Why is RuntimeException considered unchecked?

Because it extends RuntimeException, which is a subclass of Exception but not enforced by the compiler. It represents programming errors like null access or division by zero.






Q5. Can we create our own exception in the hierarchy?

Yes, by creating a custom exception class extending Exception (checked) or RuntimeException (unchecked).