Checked vs unchecked exceptions, try-with-resources, custom exceptions, and exception propagation.
Question 1: What is the difference between checked and unchecked exceptions?
Ans
Checked exceptions are subclasses of Exception, excluding RuntimeException, that the compiler forces you to either catch or declare with throws, typically representing recoverable external conditions like IOException. Unchecked exceptions extend RuntimeException and are not checked by the compiler at all, typically representing programming errors like NullPointerException or IllegalArgumentException.
Example
java
void read() throws IOException { } // checked — must declare or catch
void divide(int a, int b) { int r = a / b; } // unchecked — ArithmeticException possible, no declaration needed
Important Point
Whether an exception is checked is determined purely by its position in the class hierarchy, not by how serious it feels.
Question 2: What is the try-catch-finally flow, and when does finally not run?
Ans
Java runs the try block until it completes normally or an exception occurs; if an exception occurs, a matching catch block handles it; the finally block then runs afterward regardless of whether an exception was thrown or caught, making it the standard place for cleanup code. The main case where finally does NOT run is if the JVM terminates abruptly, such as a call to System.exit() inside the try or catch block, or if the JVM crashes.
Example
java
try { work(); }
catch (IOException e) { recover(); }
finally { close(); } // runs whether or not an exception occurred
Important Point
Try-with-resources is generally preferred over manual resource cleanup in finally, since it handles exceptions during close() more predictably.
Question 3: What is the difference between throw and throws?
Ans
throw is a statement that actually raises a specific exception instance at the point it's used. throws appears in a method's signature to declare which checked exceptions that method might propagate to its caller, without itself raising anything.
Example
java
void validate(int age) {
if (age < 0) throw new IllegalArgumentException("Invalid age"); // throw
}
void read() throws IOException { } // throws — declaration only
Important Point
A method can throw unchecked exceptions freely without listing them in throws — the declaration is only mandatory for checked exceptions.
Question 4: What is try-with-resources, and why is it preferred over manual finally cleanup?
Ans
Try-with-resources automatically closes any resource that implements AutoCloseable when the try block finishes, whether normally or via an exception, without needing an explicit finally block to call close(). It's preferred because it's harder to forget, handles multiple resources in the correct reverse closing order automatically, and correctly manages exceptions that occur during closing by suppressing them rather than hiding the original exception.
Example
java
try (BufferedReader br = new BufferedReader(new FileReader("a.txt"))) {
System.out.println(br.readLine());
} // br.close() is called automatically here
Important Point
A resource used in try-with-resources must implement AutoCloseable, or its subtype Closeable — you can't use it with an arbitrary object.
Question 5: What is finally?
Ans
finally is a block associated with exception handling that is normally executed after the try/catch processing, whether or not an exception occurs.