Compile Error for Unhandled Checked Exception
Fast recall
Quick revision
Core answer
try/catch or add throws to the method signature. Skip both and you get a compile error like 'unreported exception IOException; must be caught or declared to be thrown'. Unchecked exceptions have no such requirement.Whether you know the catch-or-declare rule is enforced at compile time, not runtime
Thinking an unhandled checked exception is a runtime error rather than a compile-time failure
Name the exact compiler message and contrast with unchecked, which compiles without any handling
Interview-ready guide
Interview answer
If you don't handle a checked exception, the code simply won't compile — and that's the key point, it's a compile-time failure, not something that blows up at runtime.
The compiler enforces what's called the catch-or-declare rule. Whenever you call something that can throw a checked exception, like reading a file which throws I O exception, you have two choices.
Either you catch it, by wrapping the call in a try-catch block and handling it there, or you declare it, by adding throws I O exception to your method's signature so that the responsibility passes up to whoever calls your method. If you do neither, the build fails with a message along the lines of unreported exception I O exception, must be caught or declared to be thrown.
So you literally can't run the program until you deal with it one way or another. That compile-time enforcement is the entire practical meaning of the word checked — the compiler won't let you silently ignore it. Compare that to unchecked exceptions, the RuntimeException subclasses: code that might throw a NullPointerException compiles perfectly fine with no handling at all, because the compiler doesn't enforce anything for those.
A couple of mistakes I'd warn against: don't just slap an empty catch block on it to make the compiler happy, because that swallows real failures silently. And don't reach for throws Exception to make it go away — declare the specific exception type instead, so the contract stays honest and callers know exactly what they're dealing with.
Compile Error for Unhandled Checked Exception
Direct Answer
The code won't compile. For a checked exception, the compiler enforces 'catch-or-declare': you must either wrap the call in a try/catch or add throws to the method signature. Skip both and you get a compile error like 'unreported exception IOException; must be caught or declared to be thrown'. Unchecked exceptions have no such requirement.
Deep Dive & Architecture
The compiler examines whether a checked exception can leave a method or constructor. If it can, the method must catch it or declare a compatible type in throws. A catch counts only when it covers the possible type, and declaring it transfers the same obligation to callers.
For example, a method calling Files.readString(path) does not compile until it catches IOException or declares it. An empty catch may satisfy the compiler but loses the failure, so compile success is not the same as correct handling. Unchecked exceptions can still be caught and documented; they simply do not take part in this compile-time rule.
Common Mistakes & Pitfalls
- Expecting a runtime error — it's a compile-time failure; the program won't build.
- Adding an empty
catchjust to compile — that silently swallows real failures. - Declaring
throws Exceptionto make it go away — declare the specific type instead. - Confusing it with unchecked —
RuntimeExceptioncode compiles with zero handling.
Check your understanding
Answer these without looking back at the article.
- Can you give examples of commonly used checked exceptions in Java?
- How do you wrap a checked exception in an unchecked exception?
- Why is InterruptedException a checked exception?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.