69

I am using HttpClient in a xamarin forms project

The class is documented, but I can not find any documentation about which exceptions its methods might throw.

For example the GetAsync Method does not have any documentation about possible exceptions. But I assume it throws, for example when the server is unreachable.

Is there somewhere a list of exceptions this class might throw?

Nathan
  • 7,099
  • 14
  • 61
  • 125
  • 1
    Yes, in the documentation of particular methods. Why do you want to know? – CodeCaster Nov 18 '16 at 12:59
  • 1
    You get a `HttpResponseMessage` which has properties `IsSuccessStatusCode` and `StatusCode` which can be one of the following: https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.118).aspx Is this what you're looking for? – Tim Schmelter Nov 18 '16 at 13:01
  • @CodeCaster I can not find any documentation about exceptions (for example here: https://msdn.microsoft.com/de-de/library/hh158944(v=vs.118).aspx). I want to know, because I want to handle all exceptions. – Nathan Nov 18 '16 at 13:03
  • @Nathan exceptions are thrown by *methods*, not classes – Panagiotis Kanavos Nov 18 '16 at 13:03
  • @PanagiotisKanavos Good point. My quesiton is inprecise in that way. I will update it. – Nathan Nov 18 '16 at 13:04
  • 3
    According to real exceptions i've only found this: _"HttpClient does not throw an exception when the HTTP response contains an error code. Instead, the IsSuccessStatusCode property is false if the status is an error code. If you prefer to treat HTTP error codes as exceptions, call HttpResponseMessage.EnsureSuccessStatusCode on the response object. This method throws an exception if the status code falls outside the range 200–299. Note that HttpClient **can throw exceptions for other reasons** — for example, if the request times out."_ A `SocketException` is another one i know. – Tim Schmelter Nov 18 '16 at 13:05
  • @TimSchmelter Thanks, that is good to know that now exceptions are thrown because of http status codes. But what about other errors? – Nathan Nov 18 '16 at 13:08
  • 9
    Since the exceptions aren't documented most likely you're going to get an incomplete list. Anyone that has called that method and gotten exceptions may tell you about the particular exceptions they got but they wouldn't know about other exceptions either. That's really what documentation is for and here Microsoft has done a bad job. – Lasse V. Karlsen Nov 18 '16 at 13:14
  • 2
    It looks like the method will wrap up things in a `HttpRequestException` and the InnerException property will contain the actual underlying exception. You get `HttpRequestException`+`WebException` if it cannot connect, if the server doesn't exist, etc. – Lasse V. Karlsen Nov 18 '16 at 13:17

1 Answers1

103

As others have commented it depend on what you are calling with HttpClient. I get what you meant though and so here are some exceptions thrown with typical method calls.

SendAsync can throw:

  • ArgumentNullException The request was null.
  • InvalidOperationException The request message was already sent by the HttpClient instance.
  • HttpRequestException The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.
  • TaskCanceledException The request timed-out or the user canceled the request's Task.

Source: Microsoft Docs -> HttpClient -> SendAsync

Similarly GetAsync PostAsync PutAsync GetStringAsync GetStreamAsync etc can throw ArgumentNullException, HttpRequestException and as above (but not InvalidOperationException).

Source: Microsoft Docs -> HttpClient -> GetAsync

Once you have called SendAsync or GetAsync etc you will have a Task<HttpResponseMessage>. Once awaited I tend to call EnsureSuccessStatusCode() to throw a HttpRequestException if there is a non success HTTP status code returned. https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs#L161

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
BritishDeveloper
  • 13,219
  • 9
  • 52
  • 62
  • [Methinks it would be prudent to catch `OperationCanceledException` instead](https://stackoverflow.com/a/34359530/159145) of `TaskCanceledException` though. – Dai Mar 26 '23 at 07:26