Why this matters
External dependencies fail unpredictably; explicit handling makes failures observable and recoverable.
Wrap network, file system, and other external calls in try/catch; add context and map to application-level errors.
External dependencies fail unpredictably; explicit handling makes failures observable and recoverable.
Side-by-side examples engineers can pattern-match during review.
var json = await http.GetStringAsync(url);try {
using var resp = await http.GetAsync(url);
if (!resp.IsSuccessStatusCode) throw new HttpRequestException($"HTTP {(int)resp.StatusCode}");
} catch (HttpRequestException ex) {
_log.Error(ex, "Fetch failed", new { url });
throw;
}await client.GetStringAsync(u); // no checkstry { await client.GetAsync(u); } catch(HttpRequestException e) { /* log */ }From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.