💥 Exceptions & Errors
15 Java concepts, each explained step by step with real code, a common trap and practice questions.
- The Throwable hierarchyThrowable → Error / Exception → RuntimeException.
- Checked vs uncheckedChecked must be handled or declared; unchecked need not be.
- try / catchCatching exceptions, catch order from specific to general.
- Multi-catchcatch (A | B e) and the rule about related types.
- finallyAlways runs (except System.exit/JVM crash); return in finally overrides.
- throw vs throwsthrow raises an exception; throws declares it in a signature.
- Custom exceptionsExtending Exception or RuntimeException, adding context.
- try-with-resourcesAutoCloseable resources closed automatically in reverse order.
- Suppressed exceptionsExceptions from close() attached via getSuppressed().
- Exception chainingWrapping causes with new X(msg, cause); getCause.
- Reading a stack traceFinding the origin line and the Caused by section.
- Common runtime exceptionsNPE, ClassCast, IllegalArgument, IllegalState, NumberFormat, IndexOutOfBounds, UnsupportedOperation, ConcurrentModification.
- Errors you should not catchOutOfMemoryError, StackOverflowError and why.
- Exception best practicesNever swallow, fail fast, don't use exceptions for flow control, preserve the cause.
- Assertionsassert statements, disabled by default, enabled with -ea.