Learn Java
import com.learnjava.exceptionhandling.CheckedUncheckedExceptions;

Checked vs Unchecked Exceptions in Java

@Concept

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.

@HowItWorks
1

Checked exceptions

Must be caught or declared with throws; the compiler enforces this, refusing to compile code that ignores the possibility.

2

Unchecked exceptions

Extend RuntimeException, and need no acknowledgment at all; the compiler doesn't require catching or declaring them.

3

What checked exceptions are meant for

Problems outside the program's control that a caller can reasonably plan for, like a missing file.

4

What unchecked exceptions are meant for

Typically actual programming mistakes, like a null reference or an invalid array index.

5

Classic examples of each

IOException and SQLException are checked; NullPointerException and ArrayIndexOutOfBoundsException are unchecked.

@Example
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());
}
}
}
Output
Unchecked exception caught: / by zero
1unsafeDivide's signature declares no throws clause at all, since ArithmeticException is unchecked and requires none.
2a / b throws that unchecked exception when b is 0, with the compiler never having required any acknowledgment of this possibility.
3main's catch block still catches it, since an unchecked exception can always be caught voluntarily, even without being required to.
4The single println call prints the caught exception's message as one line.
@UseCases
Deciding whether a new custom exception should be checked or unchecked based on whether callers can reasonably plan for it
Understanding why a method reading a file must handle IOException, but one accessing an array doesn't need to handle ArrayIndexOutOfBoundsException
Recognizing a NullPointerException as very likely a bug to fix, not a condition to routinely catch
Reading a method's throws clause to know exactly which checked exceptions it might produce

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.