Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/HotCode2025/Print-Estoy-Cansado-Jefe-TercerSemestre/llms.txt

Use this file to discover all available pages before exploring further.

Exceptions are Java’s built-in mechanism for signalling and recovering from errors at runtime. Instead of letting a bad state silently propagate, Java interrupts normal flow, hands control to the nearest matching catch block, and gives you a full stack trace to diagnose the problem. Mastering exceptions lets you write programs that fail gracefully and remain debuggable.

try-catch: Catching Errors at Runtime

The fundamental construct is the try-catch block. Code that might throw goes inside try; one or more catch clauses handle specific exception types.
TestExcepciones.java
package test;

public class TestExcepciones {
    public static void main(String[] args) {
        int resultado = 0;
        try {
            resultado = 10/0;
        }catch(Exception e) {
            System.out.println("Ocurrio un error");
            e.printStackTrace(System.out); //Se conoce como pila de excepciones
        }
        System.out.println("La variable de resultado tiene como valor: " + resultado);
    }
}
Even though 10 / 0 throws an ArithmeticException, the catch block catches it, prints the stack trace, and execution continues — resultado stays 0 instead of crashing the JVM.
Catching the generic Exception class (as above) is fine for learning, but in production code it can mask unexpected bugs. Prefer catching the most specific exception type possible so unrelated errors are not silently swallowed.

Checked vs Unchecked Exceptions

Java divides exceptions into two families:
FamilyBase classCompiler enforcementMust declare with throws?
CheckedExceptionYes — must handle or declareYes
UncheckedRuntimeExceptionNo — optionalNo

Checked exception example

OperacionExcepcion extends Exception, so the compiler forces every caller to either catch it or list it in a throws clause.
OperacionExcepcion.java (checked)
package excepciones;

public class OperacionExcepcion extends Exception {
    //Constructor
    public OperacionExcepcion (String mensaje){
        super(mensaje);
    }
}
Aritmetica.java (checked)
package aritmetica;
import excepciones.OperacionExcepcion;

public class Aritmetica {
    public static int division(int numerador, int denominador)
            throws OperacionExcepcion{
        if (denominador == 0) {
            throw new OperacionExcepcion("Division entre cero");
        }
        return numerador/denominador;
    }
}

Unchecked (RuntimeException) example

By changing the parent class to RuntimeException, the compiler no longer requires callers to handle the exception — it propagates automatically if not caught.
package excepciones;

public class OperacionExcepcion extends RuntimeException {
    //Constructor
    public OperacionExcepcion (String mensaje){
        super(mensaje);
    }
}
Use checked exceptions for recoverable conditions you expect callers to handle (e.g., invalid user input, file not found). Use unchecked exceptions for programming errors that should never happen in correct code (e.g., null pointer, illegal argument).

Custom Exception Classes

Creating your own exception class gives you meaningful names in stack traces and lets you attach domain-specific information. Follow these steps:
1

Extend Exception or RuntimeException

Choose the base class based on whether you want compile-time enforcement (checked) or not (unchecked).
OperacionExcepcion.java
package excepciones;

// Checked — callers must handle or declare it
public class OperacionExcepcion extends Exception {
    public OperacionExcepcion(String mensaje) {
        super(mensaje); // passes the message to Exception
    }
}
2

Throw the exception with throw

Use the throw keyword (singular) inside the method body where the bad condition is detected.
Aritmetica.java
if (denominador == 0) {
    throw new OperacionExcepcion("Division entre cero");
}
3

Declare it with throws

If your exception is checked, add throws OperacionExcepcion to the method signature so the compiler knows about it.
Aritmetica.java
public static int division(int numerador, int denominador)
        throws OperacionExcepcion {
    // ...
}
4

Catch it with a specific catch clause

Always list the most specific exception type first. Retrieve the message with e.getMessage().
TestExcepciones.java
try {
    resultado = division(10, 0);
} catch (OperacionExcepcion e) {
    System.out.println("Ocurrio un error de tipo OperacionExcepcion");
    System.out.println(e.getMessage());
} catch (Exception e) {
    System.out.println("Ocurrio un error inesperado");
    e.printStackTrace(System.out);
}

The finally Block

A finally block runs no matter what — whether an exception was thrown, caught, or never happened at all. Use it to release resources such as database connections, file handles, or scanners.
TestExcepciones.java — with finally
package test;

import static aritmetica.Aritmetica.division;

public class TestExcepciones {
    public static void main(String[] args) {
        int resultado = 0;
        try {
            resultado = division (10, 0);
        }catch(OperacionExcepcion e){
            System.out.println("Ocurrio un error de tipo OperacionExcepcion");
            System.out.println(e.getMessage());
        }catch(Exception e) {
            System.out.println("Ocurrio un error");
            e.printStackTrace(System.out); //Se conoce como pila de excepciones
            System.out.println(e.getMessage());
        }
        finally{
            System.out.println("Se reviso la division entre cero");
        }
        System.out.println("La variable de resultado tiene como valor: " + resultado);
    }
}
Key behaviours of finally:
  • Executes even when an exception is not caught and propagates up the call stack.
  • Executes even when the try block contains a return statement.
  • Is the right place to close Scanner, Connection, or any Closeable resource (or use try-with-resources for automatic closure).

Build docs developers (and LLMs) love