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 matchingDocumentation 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.
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 thetry-catch block. Code that might throw goes inside try; one or more catch clauses handle specific exception types.
TestExcepciones.java
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.
Checked vs Unchecked Exceptions
Java divides exceptions into two families:| Family | Base class | Compiler enforcement | Must declare with throws? |
|---|---|---|---|
| Checked | Exception | Yes — must handle or declare | Yes |
| Unchecked | RuntimeException | No — optional | No |
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)
Aritmetica.java (checked)
Unchecked (RuntimeException) example
By changing the parent class toRuntimeException, the compiler no longer requires callers to handle the exception — it propagates automatically if not caught.
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:Extend Exception or RuntimeException
Choose the base class based on whether you want compile-time enforcement (checked) or not (unchecked).
OperacionExcepcion.java
Throw the exception with throw
Use the
throw keyword (singular) inside the method body where the bad condition is detected.Aritmetica.java
Declare it with throws
If your exception is checked, add
throws OperacionExcepcion to the method signature so the compiler knows about it.Aritmetica.java
The finally Block
Afinally 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
finally:
- Executes even when an exception is not caught and propagates up the call stack.
- Executes even when the
tryblock contains areturnstatement. - Is the right place to close
Scanner,Connection, or anyCloseableresource (or use try-with-resources for automatic closure).