Why this matters
External dependencies are unreliable; explicit handling makes issues observable and recoverable.
Wrap network/filesystem/process calls; check status codes/results and add context to failures.
External dependencies are unreliable; explicit handling makes issues observable and recoverable.
Side-by-side examples engineers can pattern-match during review.
String body = client.send(request, BodyHandlers.ofString()).body();try {
HttpResponse<String> resp = client.send(request, BodyHandlers.ofString());
if (resp.statusCode() >= 400) throw new IOException("HTTP " + resp.statusCode());
} catch (IOException | InterruptedException e) {
logger.error("fetch failed url={}", url, e);
throw new RuntimeException("external call failed", e);
}Files.readString(path)try { Files.readString(path); } catch (IOException e) { /* log+wrap */ }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.