title: System.Web.Http.HttpResponseException in catch block while exception handling
body: <p>I am intentionally making a violation of a unique constraint in my database, and trying to handle an exception.</p>
  
  <p>This among else is in my form:</p>
  
  <pre><code>HttpResponseMessage response = KorisniciService.PostResponse(k);
                  if (response.IsSuccessStatusCode)
                  {
                      MessageBox.Show(Messages.add_usr_succ);
                      DialogResult = DialogResult.OK;
                      Close();
                  }
                  else
                  {
                      string message = response.ReasonPhrase;
                      if (string.IsNullOrEmpty(Messages.ResourceManager.GetString(response.ReasonPhrase)))
                          message = Messages.ResourceManager.GetString(response.ReasonPhrase);
  
  
                      MessageBox.Show("Error code: " + response.StatusCode + " Message: " + message);
                  }
  </code></pre>
  
  <p>My controller: </p>
  
  <pre><code>public IHttpActionResult PostKorisnici(Korisnici obj)
          {
              if (!ModelState.IsValid)
                  return BadRequest();
              try
              {
                  obj.KorisnikId = Convert.ToInt32(dm.esp_Korisnici_Insert(obj.Ime, obj.Prezime, obj.Email, obj.Telefon, obj.KorisnickoIme, obj.LozinkaSalt, obj.LozinkaHash, obj.Status, obj.Adresa, obj.GradId).FirstOrDefault());
              }
              catch (EntityException ex)
              {
                  throw CreateHttpResponseException(Util.ExceptionHandler.HandleException(ex), HttpStatusCode.Conflict);
              }
  
  
              foreach (var item in obj.Uloge)
              {
                      dm.esp_KorisniciUloge_Insert(obj.KorisnikId, item.UlogaId);
              }
              return CreatedAtRoute("DefaultApi", new { id = obj.KorisnikId }, obj);
          }
  </code></pre>
  
  <p>HttpResponseException making function:</p>
  
  <pre><code>private HttpResponseException CreateHttpResponseException(string reason, HttpStatusCode code)
          {
  
  
              HttpResponseMessage msg = new HttpResponseMessage()
              {
                  StatusCode = code,
                  ReasonPhrase = reason,
                  Content = new StringContent(reason)
              };
              return new HttpResponseException(Request.CreateResponse(msg));
          }
  </code></pre>
  
  <p>Exception handler class:</p>
  
  <pre><code>public class ExceptionHandler
      {
          public static string HandleException(EntityException error)
          {
  
              SqlException ex = error.InnerException as SqlException;
  
                  switch (ex.Number)
                  {
                      case 2627:
                      {
                          return GetConstraintExceptionMessage(ex);
                      }
                      default:
                          return error.Message + "(" + error +")";
                  }
          }
          /*Message "Violation of UNIQUE KEY constraint 'CS_KorisnickoIme'. Cannot insert duplicate key in object 'dbo.Korisnici'. The duplicate key value is (farish).\r\nThe statement has been terminated."    string*/
  
          private static string GetConstraintExceptionMessage(SqlException error)
          {
              string newMessage = error.Message;
              int startIndex = newMessage.IndexOf("'");
              int endIndex = newMessage.IndexOf("'", startIndex + 1);
  
              if (startIndex&gt;0 &amp;&amp; endIndex&gt;0)
              {
                  string constraintName = newMessage.Substring(startIndex + 1, endIndex - startIndex - 1);
  
                  if (constraintName == "CS_KorisnickoIme")
                      newMessage = "username_con";
                  else if (constraintName == "CS_Email")
                      newMessage = "email_con";
  
  
              }
               return newMessage;
          }
  </code></pre>
  
  <p>So when I produce an error, instead of a popup window (which shows up fine in a tutorial video) I get a <strong>System.Web.Http.HttpResponseException in a first catch block of my post method</strong> and nothing passed back to my form.</p>