What is the difference between checked and unchecked exceptions?
Checked exceptions are checked by the compiler, so you must either catch them or declare them with throws, and they usually represent recoverable problems like a missing file. Unchecked exceptions extend RuntimeException, are not checked at compile time, and usually represent programming bugs like a null pointer or an array index out of bounds.
Checked exceptions
These are conditions your program should anticipate and handle, such as a file not being found. The compiler forces you to deal with them, which is why you see try catch or a throws clause.
// Must handle or declare
void read() throws IOException {
Files.readString(Path.of("data.txt"));
}
Unchecked exceptions
These extend RuntimeException and usually mean a bug in the code. The compiler does not force you to handle them, since the right fix is to correct the code, not to catch the error.
int[] a = new int[2];
int x = a[5]; // ArrayIndexOutOfBoundsException, unchecked
A crisp answer is checked for recoverable situations the compiler forces you to handle, unchecked for programming errors that extend RuntimeException. Giving one example of each, like IOException and NullPointerException, seals it.
Common follow up questions
Related interview questions
Want the full Java guide?
Read every Java concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the Java guide All Java questions