title: How can I retrieve passed exception from HttpResponseMessage(HttpStatusCode, Exception)
body: <p>According to the following website, you can pass an exception parameter to the HttpResponseMessage.CreateErrorResponse method (<a href="https://msdn.microsoft.com/en-us/library/jj127064(v=vs.118).aspx" rel="noreferrer">https://msdn.microsoft.com/en-us/library/jj127064(v=vs.118).aspx</a>)</p>
  
  <p>My question is how can I retrieve the exception information from the HttpResponseMessage created by the CreateErrorResponse method. If there is not way to get the exception information, what is the point of having a method overload for taking an exception as an input?</p>
  
  <p>To clarify on what answers I am not looking for...
  I know that I can pass a customized error reason in the content of the body (<a href="http://weblogs.asp.net/fredriknormen/asp-net-web-api-exception-handling" rel="noreferrer">http://weblogs.asp.net/fredriknormen/asp-net-web-api-exception-handling</a>) but I am really just curious about how to use the <em>HttpRequestMessageExtensions.CreateErrorResponse Method (HttpRequestMessage, HttpStatusCode, <strong>Exception</strong>)</em> method overload. </p>
  
  <p>Example WebAPI Controller:</p>
  
  <pre><code>Route("location/{locationName}/Databases/{databasename}/ProcedureSession")][LocationSetUp]
  public HttpResponseMessage Post([FromBody] ProcedureSessionData procedureSession)
      {
          try
          {
              throw new Exception("test Exception");
          }
          catch (Exception e)
          {
             return Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
               e);
          }
      }
  </code></pre>
  
  <p>How do I pick up the exception in this code:</p>
  
  <pre><code>using (var client = new HttpClient())
    {
      client.BaseAddress = new Uri("http://localhost/");
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
      string api = "testApi/location/here/databases/testDb/ProcedureSession";
      HttpResponseMessage response = client.PostAsJsonAsync(api, newSessionData).Result;
      if (!response.IsSuccessStatusCode)
      {
           //how can i pick up the exception object from here? 
           //Or am I missing the point of this CreateErrorResponse overload?
      }
  </code></pre>