Short Answer:
Exception handling in Java is a mechanism to handle runtime errors so that normal program flow is maintained.
It prevents program crashes and allows graceful error recovery using try, catch, finally, throw, and throws.
1️⃣ Exception Hierarchy
Object
|
Throwable
/ \
Error Exception
|
RuntimeException
2️⃣ Types of Exceptions
Checked Exceptions
- Checked at compile time
- Must handle using try-catch or throws
- Example: IOException, SQLException
Unchecked Exceptions
- Occur at runtime
- Not mandatory to handle
- Example: NullPointerException, ArithmeticException
Errors
- Serious problems
- Cannot be handled normally
- Example: OutOfMemoryError
3️⃣ Exception Handling Keywords
| Keyword | Purpose |
| try | Block where exception may occur |
| catch | Handle exception |
| finally | Always executes |
| throw | Explicitly throw exception |
| throws | Declare exception |
4️⃣ Basic Example
public class Test {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Finally block executed");
}
}
}
5️⃣ Important Concepts
- Multiple catch blocks allowed.
- Specific exception must come before generic (Exception).
- finally block executes even if exception not handled.
- try-with-resources automatically closes resources.
Try-With-Resources Example
try (FileReader fr = new FileReader("test.txt")) {
// read file
} catch (IOException e) {
e.printStackTrace();
}
6️⃣ Custom Exception
class InvalidAgeException extends Exception {
InvalidAgeException(String message) {
super(message);
}
}
Exception Propagation
If exception is not handled in current method, it propagates to caller method.
void m1() {
m2();
}
void m2() {
int a = 10 / 0; // propagates to m1
}
Best Practices
- Catch specific exceptions.
- Do not use empty catch blocks.
- Log exceptions properly.
- Use custom exceptions for business logic.
- Do not use exceptions for normal flow control.
Frequently Asked Interview Questions (With Answers)
1️⃣ Difference between Checked and Unchecked Exception?
Short: Checked are compile-time; Unchecked are runtime.
- Checked: Must handle using try-catch or throws (IOException).
- Unchecked: Occur at runtime (NullPointerException).
2️⃣ Difference between throw and throws?
Short: throw is used to explicitly throw an exception; throws declares exception.
void test() throws IOException { } // throws
throw new ArithmeticException(); // throw
3️⃣ Can we have try without catch?
Short: Yes, but must have finally block.
try {
int a = 10;
} finally {
System.out.println("Executed");
}
4️⃣ What happens if exception occurs in finally block?
Short: Finally exception overrides original exception.
If both try and finally throw exceptions, the finally exception suppresses the try exception.
5️⃣ Can we override method and throw broader exception?
Short: No.
Child class cannot throw broader checked exception than parent method. It can throw:
- Same exception
- Subclass exception
- Unchecked exception
6️⃣ What is try-with-resources?
Short: Automatically closes resources.
Introduced in Java 7. Resources must implement AutoCloseable.
7️⃣ Difference between Error and Exception?
Short: Error is serious JVM issue; Exception is application-level issue.
- Error: OutOfMemoryError (not recoverable)
- Exception: NullPointerException (recoverable)
8️⃣ Is finally always executed?
Short: Yes, except in rare cases.
finally will not execute if:
- System.exit() is called
- JVM crashes
9️⃣ What is Stack Trace?
Short: It shows method call sequence when exception occurs.
It helps debug by showing where exception happened.
🔟 How Exception Propagation Works?
Short: Exception moves up call stack until handled.
void m1() {
m2();
}
void m2() {
int a = 10 / 0; // Propagates to m1
}
If not handled anywhere → JVM terminates program.
Bonus Tricky Questions
- Can constructor throw exception? → Yes.
- Can static block throw exception? → Yes, but must handle.
- Can finally contain return statement? → Yes, but not recommended.
- What is suppressed exception? → Happens in try-with-resources.
- Can we catch multiple exceptions in one catch? → Yes (Java 7 multi-catch).
0 comments