You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Errors happen — files are missing, networks time out, users provide invalid input. Java's exception handling mechanism gives you a structured way to detect, respond to, and recover from errors. This lesson also covers reading and writing files.
Throwable
├── Error (serious — don't catch: OutOfMemoryError, StackOverflowError)
└── Exception
├── IOException (checked)
├── SQLException (checked)
├── FileNotFoundException (checked)
└── RuntimeException (unchecked)
├── NullPointerException
├── ArrayIndexOutOfBoundsException
├── IllegalArgumentException
├── ArithmeticException
└── ClassCastException
| Type | Checked | Unchecked (Runtime) |
|---|---|---|
| Must handle? | Yes — compiler enforces try/catch or throws | No — compiler does not enforce |
| Caused by | External factors (I/O, network, database) | Programming errors (null access, bad cast) |
| Extends | Exception (but not RuntimeException) | RuntimeException |
| Examples | IOException, SQLException | NullPointerException, ArithmeticException |
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
} finally {
System.out.println("This always executes");
}
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.