title: How to properly handle Task Exception
body: <p>I am trying to figure out how to properly handle some exceptions in a Task:</p>
  
  <p><strong>Calling Method:</strong>            </p>
  
  <pre><code>Task&lt;Branch3GInfo&gt; getActive3GRoutersTask = 
      Task.Run(
          () =&gt; CAS.Service.GetBranch3GInformationAsync(3)
      );
  
  try
  {
      Task.WaitAll(discoverRouterExtendedInfoTask, getActive3GRoutersTask);
  }
  catch(AggregateException aggEx)
  {
      StringBuilder sb = new StringBuilder();
      sb.Append("Some problems occured on GetNodesInCriticalCondition(): \n");
      foreach (var ex in aggEx.InnerExceptions)
      {
          sb.Append(ex.Message);
      }
  
      Trace.TraceError(sb.ToString());
  }
  
  var branch3gInfo = getActive3GRoutersTask.Result;
  </code></pre>
  
  <p><code>var branch3gInfo = getActive3GRoutersTask.Result;</code> throws a <code>System.Aggregate Exception</code>.</p>
  
  <p><strong>Called method:</strong></p>
  
  <pre><code>public async Task&lt;Branch3GInfo&gt; GetBranch3GInformationAsync(int searchDepth)
  {
      var branch3gInfo = await GetBranch3GInterfacesAsync();
  
      var activeRoutersExtInfos = new List&lt;CasApiRouterModelExtendedInfo&gt;();
      foreach (var netInterface in branch3gInfo.Branch3GActiveInterfaces)
      {
          try
          {
              var task = GetRouterExtendedInfoFromInterfaceAsync(netInterface, searchDepth);
              var result = await task;
  
              if (task.Status == TaskStatus.RanToCompletion &amp;&amp; result != null)
              {
                  activeRoutersExtInfos.Add(result);
              }
              else
              {
                  Trace.TraceError("Error occured trying to get info about Active 3G Router " + netInterface.ModelName + ".");
              }
          }
          catch (Exception ex)
          {
              Trace.TraceError("Error occured trying to get info about Active 3G Router " + netInterface.ModelName +
                  ": \n" + ex.Message);
          }
  
      }
  
      return new Branch3GInfo()
      {
          Branch3GActiveInterfaces = branch3gInfo.Branch3GActiveInterfaces,
          Branch3GCapableInterfaces = branch3gInfo.Branch3GCapableInterfaces,
          Branch3GActiveRouters = activeRoutersExtInfos.ToArray()
      };
  }
  </code></pre>
  
  <p>From my code, I am making the assumption that <code>GetBranch3GInformationAsync()</code> should be either executing the <code>else</code> clause which doesn't add the result to <code>activeRoutersExtInfos</code>, OR, it should be catching the exception.  Why is it that the calling method throws a <code>System.AggregateException</code> if I am handling this in <code>GetBranch3GInformationAsync()</code>?</p>
  
  <p>How do I go about fixing this so that any call to <code>GetRouterExtendedInfoFromInterfaceAsync()</code> that fails due to an exception is ignored, and so that it doesn't break my code?</p>
  
  <p>UPDATE:
  Stack trace for the <code>InnerException</code> in <code>System.AggregateException</code> ("The remote server returned an error: (500) Internal Server Error.")</p>
  
  <pre><code>at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 
  at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at CASpectrumApi.CasApiRestCall.&lt;ExecuteAsync&gt;d__38.MoveNext() in C:\src\CASpectrumApi\src\CASpectrumApi\Queries\CasApiRestCall.cs:line 294
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at CASpectrumApi.Managers.CasApiServiceManager.&lt;RenderAndExecuteRestCallAsync&gt;d__103.MoveNext() in C:\src\CASpectrumApi\src\CASpectrumApi\Managers\CasApiServiceManager.cs:line 2516
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at CASpectrumApi.Managers.CasApiServiceManager.&lt;QueryModelsFilterByAttributeNotDeviceOnlyAsync&gt;d__51.MoveNext() in C:\src\CASpectrumApi\src\CASpectrumApi\Managers\CasApiServiceManager.cs:line 1196
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at CASpectrumApi.Managers.CasApiServiceManager.&lt;GetBranch3GInterfacesAsync&gt;d__34.MoveNext() in C:\src\CASpectrumApi\src\CASpectrumApi\Managers\CasApiServiceManager.cs:line 442
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at CASpectrumApi.Managers.CasApiServiceManager.&lt;GetBranch3GInformationAsync&gt;d__26.MoveNext() in C:\src\CASpectrumApi\src\CASpectrumApi\Managers\CasApiServiceManager.cs:line 246
  </code></pre>