📊 Capítulo 11: Tratamento Robusto de Exceções
Especialização em Sistemas com Java
🗺️ A Hierarquia de Classes de Exceção da JVM
flowchart TD
TH["Throwable (Raiz de todos os erros e exceções)"]
--> ERR["Error (Falhas fatais da JVM: OutOfMemoryError, StackOverflowError / Não trate!)"]
TH --> EXC["Exception"]
EXC --> CHECKED["Checked Exceptions (IOException, SQLException)\n(O compilador OBRIGA a tratar ou declarar 'throws'!)"]
EXC --> UNCHECKED["RuntimeException (Unchecked Exceptions: NullPointerException, IllegalArgumentException)\n(Erros de lógica de programação / Trate defensivamente!)"]
style TH fill:#ede7f6,stroke:#512da8,stroke-width:2px
style ERR fill:#ffebee,stroke:#d32f2f,stroke-width:2px
style EXC fill:#fff3e0,stroke:#ff9800,stroke-width:2px
style CHECKED fill:#e1f5fe,stroke:#03a9f4,stroke-width:2px
style UNCHECKED fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
🎯 Slide 1: O Moderno try-with-resources
// Qualquer classe que implemente java.lang.AutoCloseable fecha automaticamente!
try (BufferedReader br = new BufferedReader(new FileReader(caminho))) {
String linha;
while ((linha = br.readLine()) != null) {
System.out.println(linha);
}
} catch (IOException e) {
System.err.println("Erro ao ler arquivo: " + e.getMessage());
}
// O arquivo é fechado com 100% de garantia mesmo se ocorrer uma exceção! ⚡
🎯 Slide 2: Exceções Customizadas de Domínio
public class SaldoInsuficienteException extends RuntimeException {
public SaldoInsuficienteException(String msg) {
super(msg);
}
}
- Torna o código da camada de negócios legível e independente de detalhes técnicos de infraestrutura.
🎯 Slide 3: Multi-Catch Block
catch (IOException | SQLException e) { ... }: Captura múltiplos tipos de erro com tratamento comum sem repetição de código.
🔗 Navegação do Capítulo 11
| 📖 Ler Conteúdo Completo | 🧠 Fazer Quiz | 💻 Ver Exemplos | 🧩 Exercícios |
Slide 1 de 1
Atalhos: ← → Navegar • Espaço Avançar • F Tela Cheia • Home Início • End Fim