Checked vs Unchecked Exceptions in Java
A checked exception is one the compiler forces calling code to acknowledge — either catch it or declare it with throws — and it's meant for problems outside the program's control that a caller can reasonably be expected to plan for, like a file that might not exist. An unchecked exception, which in Java means anything extending RuntimeException, needs no such acknowledgment at all; it typically represents a programming mistake, like passing a null where an object was required, or accessing an array index that doesn't exist.
IOException and SQLException are classic checked exceptions, since a file read or database query can genuinely fail for reasons entirely outside the program's control that a caller should plan for. NullPointerException and ArrayIndexOutOfBoundsException are classic unchecked ones, since they almost always point to an actual bug rather than an expected, external failure condition.
Checked exceptions
Must be caught or declared with throws; the compiler enforces this, refusing to compile code that ignores the possibility.
Unchecked exceptions
Extend RuntimeException, and need no acknowledgment at all; the compiler doesn't require catching or declaring them.
What checked exceptions are meant for
Problems outside the program's control that a caller can reasonably plan for, like a missing file.
What unchecked exceptions are meant for
Typically actual programming mistakes, like a null reference or an invalid array index.
Classic examples of each
IOException and SQLException are checked; NullPointerException and ArrayIndexOutOfBoundsException are unchecked.
public class CheckedUncheckedDemo { static void unsafeDivide(int a, int b) { System.out.println(a / b); } public static void main(String[] args) { try { unsafeDivide(10, 0); } catch (ArithmeticException e) { System.out.println("Unchecked exception caught: " + e.getMessage()); } }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.