Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is the difference between checked and unchecked exceptions? Give an example of each from the JDK and explain when to use which.

A checked exception must be caught or declared with throws, and the compiler enforces it; IOException and SQLException are examples. An unchecked exception is a RuntimeException or one of its subclasses, such as IllegalArgumentException or NullPointerException, and need not be declared. We use checked exceptions for recoverable conditions the caller is expected to handle, and unchecked exceptions for programming errors and violated preconditions; the checked rule is enforced only by javac, not by the JVM.

Why this question is asked

Checked exceptions are one of the first things that feel unusual about Java, especially for developers coming from other languages. Interviewers ask about them because choosing the wrong kind leads to code that is either full of empty catch blocks or crashes on conditions it should have handled. In this answer, we will see how the compiler treats each kind, look at JDK examples of both, work out when to use which with a small example, and finish with a detail many people miss: the JVM itself does not know about checked exceptions.

1How the Compiler Treats Each Kind

Java divides exceptions by their place in the Throwable hierarchy. Any subclass of Exception that is not a subclass of RuntimeException is checked. RuntimeException and its subclasses are unchecked, and so are Errors.

For a checked exception, the compiler applies the catch or declare rule: if a method's body can throw it, the method must either catch it or list it in its throws clause, and every caller then faces the same choice. For an unchecked exception the compiler says nothing; we may still catch it or document it, but nothing forces us.

Java
String firstLine(Path p) {
return Files.readAllLines(p).get(0); // error: unreported exception IOException
}
String firstLine2(Path p) throws IOException {
return Files.readAllLines(p).get(0); // ok: the caller must handle IOException
}
int parse(String s) {
return Integer.parseInt(s); // ok: NumberFormatException is unchecked
}
parse("abc"); // NumberFormatException at run time
Checked and unchecked exceptions
CheckedUnchecked
SuperclassException, not RuntimeExceptionRuntimeException (or Error)
Compiler rulemust catch or declarenothing enforced
JDK examplesIOException, SQLExceptionIllegalArgumentException, NullPointerException
Usually meansthe outside world failedthe code has a bug
Typical responseretry, fall back, tell the userfix the calling code

2Examples From the JDK

IOException is the classic checked exception. Reading a file can fail because the file was deleted, the disk is full or the network share went away, no matter how correct our code is. SQLException is another: the database may be down or a constraint may be violated. InterruptedException, thrown by Thread.sleep and blocking queue methods, is checked so that callers must decide what to do when a thread is asked to stop.

IllegalArgumentException is the classic unchecked exception: a method received an argument it does not accept, such as a negative size. NullPointerException means code used a null reference. IndexOutOfBoundsException, IllegalStateException and ArithmeticException (division by zero) are others. In each case, the caller could have avoided the exception by calling the method correctly.

3When to Use Which

The usual guideline is: use a checked exception when the condition is recoverable and a reasonable caller is expected to handle it; use an unchecked exception when it signals a programming error or a violated precondition. Consider a bank transfer service.

Java
public class InsufficientFundsException extends Exception { // checked
public InsufficientFundsException(String msg) { super(msg); }
}
public void transfer(Account from, Account to, long cents) throws InsufficientFundsException {
if (cents <= 0) throw new IllegalArgumentException("amount must be positive: " + cents);
Objects.requireNonNull(to, "to"); // NullPointerException if null
if (from.balance() < cents) throw new InsufficientFundsException("balance too low");
...
}
try {
service.transfer(a, b, 5_000);
} catch (InsufficientFundsException e) {
showMessage("Not enough money in the account"); // expected, handled
}
Step throughChoosing the kind of exception
  1. Could correct calling code have prevented it?
    YesUnchecked: IllegalArgumentException, IllegalStateExceptionNoGo on
  2. Is the caller expected to handle it and can it recover?
    YesChecked exception (or an explicit result)NoUnchecked, handled at a boundary

2 steps in order. Play them, or step through them one at a time.

Notice the difference. A negative amount or a null account is a bug in the caller, so no caller should catch it; it should be fixed. Insufficient funds is a normal business outcome that the caller must handle, so making it checked forces every caller to think about it. Many modern codebases model that kind of outcome with a result type or an unchecked exception documented in the API instead, which is a design debate covered in other questions, but the distinction between expected conditions and bugs is the same.

4The JVM Does Not Check Checked Exceptions

The catch or declare rule exists only in the Java compiler. In bytecode, the throws clause is just metadata; the JVM will happily let any method throw any Throwable. That is how other JVM languages such as Kotlin can call Java methods without handling their checked exceptions, and how code compiled from Kotlin can throw an IOException from a method with no throws clause.

It is also possible from Java itself, with a well-known generic trick often called a sneaky throw, which works because the cast to a type variable is erased.

Java
@SuppressWarnings("unchecked")
static <E extends Throwable> void sneakyThrow(Throwable t) throws E {
throw (E) t; // erased: no cast happens at run time
}
static void noThrowsClause() {
Main.<RuntimeException>sneakyThrow(new IOException("disk")); // compiles
}
// noThrowsClause() throws java.io.IOException, though it declares nothing

So code at boundaries, such as a framework's error handler, must be ready for checked exceptions arriving where the signatures say they cannot. Libraries like Lombok's @SneakyThrows use this trick on purpose, but in normal application code it is best avoided because it hides real failures from callers.

5Follow-up questions

Can a catch block catch a checked exception the try block cannot throw?

No, the compiler rejects catch (IOException e) if nothing in the try block can throw IOException, reporting that the exception is never thrown in the body. It allows catching Exception or Throwable, because those also cover unchecked exceptions.

Should we catch NullPointerException?

Almost never. It means a bug, and catching it hides the bug and leaves the program in a state nobody planned for. The fix is to find where the null came from, or to validate inputs early with Objects.requireNonNull.

What happens if an overriding method declares a broader checked exception?

It does not compile. An override may throw fewer or narrower checked exceptions than the method it overrides, never new or broader ones, because callers using the parent type would not be prepared for them.

6Conclusion

In this answer, we saw that checked exceptions, subclasses of Exception outside RuntimeException such as IOException and SQLException, must be caught or declared, while unchecked exceptions, RuntimeException subclasses such as IllegalArgumentException and NullPointerException, need not be. We use checked exceptions for recoverable conditions a caller is expected to handle and unchecked ones for programming errors and violated preconditions, and we saw that only javac enforces the rule, so the JVM lets any method throw any Throwable.

What to say in the interviewAbout 90 seconds aloud

Open with

A checked exception is one the compiler forces me to deal with: if my method can throw it, I either catch it or declare it in the throws clause, and callers face the same choice.

Then, if they want more

Checked exceptions are subclasses of Exception that are not RuntimeExceptions; IOException and SQLException are the usual JDK examples, and InterruptedException is another. Unchecked exceptions are RuntimeException and its subclasses, like IllegalArgumentException, NullPointerException or IllegalStateException, plus Errors, and the compiler does not require any handling. The guideline for choosing is about who is at fault and who can recover.

If the problem comes from the outside world, like a missing file or a database being down, and the caller can reasonably do something, retry, fall back, show a message, a checked exception makes that explicit.

If it means the caller made a mistake, like passing a negative amount or null, it is a programming error, so I throw an unchecked exception and the caller should fix their code rather than catch it. One detail worth knowing is that the checked rule is only enforced by javac.

The JVM does not check throws clauses at all, so Kotlin code, or a sneaky throw trick using generics, can throw a checked exception from a method that does not declare it, and boundary handlers should be ready for that.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between checked and unchecked exceptions? Give an example of each...": https://scrivio-interview-library.netlify.app/java/exceptions/ff7d648c08/

More in Exceptions and error handling