Interview Question

Checked vs unchecked exceptions?

Checked exceptions require catch-or-declare; unchecked exceptions do not.

💡 Concept ✅ Quick Revision ☕ Java

Answer

Checked exceptions are exception classes subject to compile-time catch-or-declare checking; unchecked exceptions are RuntimeException, Error, and their subclasses. • A method must catch or declare checked exceptions it can throw. • Unchecked exceptions do not require a throws declaration. • The distinction is based on the exception class hierarchy.

Example

Code
void read() throws java.io.IOException {
    throw new java.io.IOException("failed");
}

Quick Revision

Checked exceptions require catch-or-declare; unchecked exceptions do not.