FAQ Part 5: Exception Handling and Memory Management

What is exception handling?

Exception handling allows you to manage runtime errors using:

  • try, catch, finally, throw, throws
try {
  int a = 5 / 0;
} catch (ArithmeticException e) {
  System.out.println("Cannot divide by zero");
}

What’s the difference between checked and unchecked exceptions?

  • Checked Exceptions: Caught or declared (e.g., IOException)
  • Unchecked Exceptions: Occur at runtime (e.g., NullPointerException)

What is garbage collection?

Java automatically deallocates memory for unused objects using garbage collection. It improves memory efficiency and reduces memory leaks.