Always catch specific exception types rather than using general catch blocks. This improves error handling precision, enables targeted recovery strategies, and maintains consistency across your codebase.
Always catch specific exception types rather than using general catch blocks. This improves error handling precision, enables targeted recovery strategies, and maintains consistency across your codebase.
When implementing error handling:
// Avoid:
try {
var httpStatusCode = await Connection.Delete(endpoint, new object(), AcceptHeaders.InvitationsApiPreview);
return httpStatusCode == HttpStatusCode.NoContent;
} catch {
return false; // Swallows ALL exceptions, including unexpected ones
}
// Preferred:
try {
var httpStatusCode = await Connection.Delete(endpoint, new object(), AcceptHeaders.InvitationsApiPreview);
return httpStatusCode == HttpStatusCode.NoContent;
} catch (NotFoundException) {
// Only handle the specific case we expect and understand
return false;
}
// When detecting specific error conditions, create custom exceptions:
if (body.Contains("secondary rate limit")) {
// Don't rely on string matching
throw new SecondaryRateLimitExceededException();
}
Catching specific exceptions ensures your error handling is predictable and that unexpected errors are properly propagated rather than silently ignored. This leads to more robust code that fails fast when encountering truly exceptional conditions.
Enter the URL of a public GitHub repository