Why this matters
Specific types enable precise handling, better logging, and safer retries.
Throw and catch the most specific exception type available; avoid catching or throwing System.Exception unless rethrowing.
Specific types enable precise handling, better logging, and safer retries.
Side-by-side examples engineers can pattern-match during review.
catch (Exception ex) { /* handle all */ }catch (InvalidOperationException ex) { /* corrective action */ }
catch (HttpRequestException ex) { /* retry/backoff */ }throw new Exception("failed");throw new InvalidOperationException("invalid state");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.