# Exceptions — Core Java

Source: https://www.geekswithgeeks.com/en/core-java/java-exceptions

> Handle failures with try / catch / finally, tell checked from unchecked exceptions, free resources with try-with-resources, and never swallow errors silently.

## Checked vs unchecked

**Checked** exceptions (subclasses of `Exception`, not `RuntimeException`) must be declared with `throws` or caught — the compiler enforces it, e.g. `IOException`. **Unchecked** (`RuntimeException`, e.g. `NullPointerException`) usually signal bugs and need no declaration.

## try-with-resources

Anything implementing `AutoCloseable` declared in the `try (...)` header is closed automatically, in reverse order, even if an exception is thrown. `finally` still runs for other cleanup.

```java
try (var reader = Files.newBufferedReader(path)) {
    return reader.readLine();
} catch (IOException e) {
    throw new UncheckedIOException(e);
}
```

## Don't swallow exceptions

An empty `catch` block hides failures and makes bugs invisible. At minimum log it with the stack trace, or rethrow wrapped with context.
