📊 Capítulo 14: Exceções & Exception Filters
Especialização em Sistemas com C#
🗺️ Filtros de Exceção Elegantes com a Cláusula when
flowchart TD
TRY["Bloco try { ... }"]
--> C1["catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)\n(Captura APENAS 404 sem desmontar o stack trace dos outros erros!) ⚡"]
TRY --> C2["catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)\n(Captura erro 401 e renova token OAuth)"]
TRY --> C_GEN["catch (Exception ex)\n(Captura genérica de fallback com log de auditoria)"]
style TRY fill:#ede7f6,stroke:#512da8,stroke-width:2px
style C1 fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
style C2 fill:#fff3e0,stroke:#ff9800,stroke-width:2px
style C_GEN fill:#ffebee,stroke:#d32f2f,stroke-width:1.5px
🎯 Slide 1: Por Que Usar when em Vez de if Interno?
-
O filtro
whené avaliado antes do Stack Unwinding acontecer! - Se a condição do
whenfor falsa, a pilha de execução original permanece 100% intacta, preservando a linha de erro original para logs de telemetria!
🎯 Slide 2: Re-Throw Correto: throw; vs throw ex;
-
❌ Destruidor de Logs:
throw ex;reseta a pilha, fazendo parecer que o erro nasceu na linha do catch! - ✅ Padrão Correto:
throw;preserva todo o histórico desde a origem do disparo original!
🎯 Slide 3: Helpers Nativos do .NET 8
// Uma única linha limpa e sem alocação:
ArgumentNullException.ThrowIfNull(usuario);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(limite);
🔗 Navegação do Capítulo 14
| 📖 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