1. The Throwable Family Tree
All anomalies in Java descend from Throwable. The Error branch models catastrophic JVM failures (do not catch). The Exception branch splits into Checked (compiler-enforced) and RuntimeException (unchecked logic bugs).
// Checked: Obligación sintáctica
public void leerArchivo() throws IOException {
new FileInputStream("datos.csv");
} 2. Anatomy of an Exception in Memory
An exception is not a simple flag: it is a FULL OBJECT allocated in the Heap. Instantiating it takes an exact snapshot of the Call Stack (StackTrace), a descriptive message, and an optional root cause.
if (saldo < monto) {
throw new SaldoInsuficienteException(
"Saldo insuficiente: requerido " + monto
);
} 3. Call Stack Unwinding Simulator
When an exception is thrown and the method has no catch block, its execution frame is destroyed and the exception propagates to the caller, bubbling up until caught or terminating the thread.
try {
servicio.procesarOrden();
} catch (SQLException e) {
// main() captura el error y rescata el hilo
LOGGER.error("Fallo en BD: " + e.getMessage());
} 4. State Machine: try-catch-finally
The finally block is GUARANTEED to execute: when try finishes cleanly, when an exception is caught, and even when an uncaught exception keeps bubbling up.
try {
// operacion()
} catch (Exception e) {
// manejo()
} finally {
conexion.cerrar(); // Siempre garantizado
} 5. try-with-resources and AutoCloseable
Since Java 7, any class implementing AutoCloseable can be opened within try parentheses. The JVM guarantees calling close() automatically upon exiting the block, even when exceptions occur.
// Varios recursos separados por punto y coma
try (var in = new FileInputStream(f1);
var out = new FileOutputStream(f2)) {
in.transferTo(out);
} 6. Custom Exceptions and Chained Causes
Translate low-level technical infrastructure faults into domain concepts using the cause constructor: throw new PaymentFailedException("Charge failed", rootCause). This retains the full trace without leaking raw internals.
public class PagoException extends Exception {
public PagoException(String msg, Throwable cause) {
super(msg, cause); // Encadenamiento
}
} 7. 4 Anti-Patterns to Avoid
Mistakes that ruin observability and cause silent disasters in production. Learn to detect and avoid them.
// O lo manejás de verdad, o lo dejás subir
try {
debitar();
} catch (SaldoInsuficienteException e) {
notificarCliente(e.getMessage()); // Acción concreta
}