Context:
In System.Net.Http HttpResponseMessage
Dotnet has offered a method EnsureSuccessStatusCode()
. I know, It's a convenient method that throws an exception if the HTTP response status code indicates an error.
But once I read in the C# book (Microsoft Exam 70-483 Second Edition) they suggested not every time throw exceptions for obvious error handling but rather keep it for unforeseen scenarios. It's crucial to strike a balance and use exceptions judiciously, reserving them for exceptional situations. Propagating exceptions again and again by breaking the application flow can negatively impact the overall performance of the application..., becomes harder for profiling tools to identify the root cause of performance issues... and many more, which I believe makes pretty sense.
Question:
I am just wondering if it's such a bad practice then why Dotnet has offered such a method? Isn't it good to make use of
IsSuccessStatusCode
and build an error-handling mechanism, like return codes or error flags? which might be more efficient.