Exceptions in Java

From the Java cheat sheet · Exception Handling · verified Jul 2026

Exceptions

try/catch/finally, resources, and custom exceptions.

java
// Quick Reference
try {
    risky();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    cleanup();
}
💡 Checked exceptions (IOException) must be caught or declared with throws.
⚡ try-with-resources auto-closes anything implementing AutoCloseable - no finally needed.
📌 finally always runs - use it for cleanup, but prefer try-with-resources for closing.
🟢 Extend RuntimeException for unchecked custom errors; Exception for checked ones.
exceptionstry-catch
Back to the full Java cheat sheet