← Java Programming learning path
Java Programming · Module 9 of 10

Exceptions

Handle errors with try, catch, finally and throw.

📖 Key ideas

  1. An exception is an error event that interrupts normal flow.
  2. try { } catch (Exception e) { } handles it.
  3. finally always runs (for cleanup).
  4. Checked exceptions (IOException) must be handled or declared with throws.
  5. Unchecked exceptions (NullPointerException) come from bugs.
💡 Real-life example: try/catch is a safety net under a trapeze: the show continues even if someone slips.
tryCode that might fail
catchHandles an exception
finallyAlways runs
throwsDeclares exceptions a method may throw