2

if you have the following methods:

public async Task<string> GetTAsync(url)
{
    return await httpClient.GetStringAsync(url); 
}

public async Task<List<string>> Get(){
   var task1 = GetTAsync(url1);
   var task2 = GetTAsync(url2);
   await Task.WhenAll(new Task[]{task1, task2}); 
   // but this may through if any  of the   tasks fail.
   //process both result
}

How can I handle exception? I looked at the documentation for HttpClient.GetStringAsync(url) method and the only exception it might throw seems to be ArgumentNullException. But at least I run into forbidden error once and would like to handle all possible exception. But i can't find any specific exceptions. Should I catch just Exception exception here? I would appreciate if it is more specific. Please help, its really important.

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
user3818435
  • 707
  • 2
  • 9
  • 16
  • Catch `Exception`, then check if it is of `AggregateException` type. If so, `AggregateException.InnerExceptions` gives you access to exceptions possibly thrown by individual tasks. Note `AggregateException` can be nested, you can use `AggregateException.Flatten` to account for this. Alternatively, after `await Task.WhenAll` you can just access `task.Result` or do `await task` on individual tasks, it will re-throw the task's exception. Related: http://stackoverflow.com/q/24623120/1768303. – noseratio Jul 11 '14 at 02:59
  • Yes i can catch aggregate exception and can flatten it to but what I want is the specific exception httpclient.GetStringAsync() method might throw. While looking other posts, someone wrote HttpRequestException is the exception that will be thrown. I can't confirm it with anything so far. – user3818435 Jul 11 '14 at 03:06
  • 1
    Why don't you try yourself? You'll get `System.Net.Http.HttpRequestException` with the relevant error info, e.g. "Response status code does not indicate success: 404 (Not Found)." Keep in mind, statuses like `404` would throw an error for `HttpClient.GetStringAsync`, but not for `HttpClient.GetAsync`. – noseratio Jul 11 '14 at 03:35
  • I tried but didn't get either HttpRequestException or Aggregate exception. But i got InvalidOperationException though. I don't know if that is what expected of when you get 404 or 403 errors. – user3818435 Jul 11 '14 at 20:05
  • When I call `GetStringAsync` with an invalid URL, I get `HttpRequestException`. It's hard to tell why you're getting `InvalidOperationException` without seeing the actual code calling `GetStringAsync` and having access to the URL. Use Fiddler to spy on the HTTP packets. – noseratio Jul 12 '14 at 00:40
  • I have the code above. You can use any url that will return either 403 or 404 for url1 - and use any valid url for url2. I don't have any other statements that might cause InvalidOperationException. The code is basically what is shown above. – user3818435 Jul 12 '14 at 20:27

1 Answers1

1

Finally i figured it as follows:

public async Task<List<string>> Get()
{
   var task1 = GetTAsync(url1);
   var task2 = GetTAsync(url2);
   var tasks = new List<Task>{task1, task2};
   //instead of calling Task.WhenAll and wait until all of them finishes 
   //and which messes me up when one of them throws, i got the following code 
   //to process each as they complete and handle their exception (if they throw too)
   foreach(var task in tasks)
   {
      try{
       var result = await task; //this may throw so wrapping it in try catch block
       //use result here
      }
      catch(Exception e) // I would appreciate if i get more specific exception but, 
                         // even HttpRequestException as some indicates couldn't seem 
                         // working so i am using more generic exception instead. 
      {
        //deal with it 
      }
   } 
}

This is a much better solution i finally figured. If there is something better, i would love to hear about it. I'm posting this -just case someone else runs into the same issue.

superjos
  • 12,189
  • 6
  • 89
  • 134
user3818435
  • 707
  • 2
  • 9
  • 16
  • 2
    What do you mean when you say `HttpRequestException` is not working? what other exception are you observing? The only other exception I got from it that I can recall was `TaskCanceledException` (which is a bug in HttpClient, see https://social.msdn.microsoft.com/Forums/en-US/d8d87789-0ac9-4294-84a0-91c9fa27e353/bug-in-httpclientgetasync-should-throw-webexception-not-taskcanceledexception?forum=netfxnetcom). That thread also implies `WebException` is a possibility so I guess it can't hurt to check for that too. – Ohad Schneider Jul 19 '15 at 09:27
  • This makes sense to me. The [MSDN doc on GetAsync](https://msdn.microsoft.com/en-us/library/hh158944(v=vs.118).aspx) not indicate that `await GetAsync` might throw a connection error. So thank you for this problem and solution, very helpful. – JJ Stiff Aug 22 '16 at 20:15