title: Exception Handling in Web API and return JSON
body: <p>I have the controller of a Web API which queries the DB and returns result in JSON is like below,</p>
  
  <pre><code>    [HttpGet]
      public HttpResponseMessage Getdetails(string ROOM, DateTime DOB_GT)
      {
          if (string.IsNullOrEmpty(ROOM))
          {
              return Request.CreateResponse(new { error = "Input paramete cannot be Empty or NULL" });
          }
  
          try
          {
              List&lt;OracleParameter&gt; prms = new List&lt;OracleParameter&gt;();
              List&lt;string&gt; selectionStrings = new List&lt;string&gt;();
              prms.Add(new OracleParameter("ROOM", OracleDbType.Varchar2, ROOM, ParameterDirection.Input));
              prms.Add(new OracleParameter("DOB_GT", OracleDbType.Date, DOB_GT, ParameterDirection.Input));
              string connStr = ConfigurationManager.ConnectionStrings["TBaseConnection"].ConnectionString;
              using (OracleConnection dbconn = new OracleConnection(connStr))
              {
                  DataSet userDataset = new DataSet();
                  var strQuery = "SELECT * from RESULTS_VW where ROOM = :ROOM and DOB &gt; :DOB_GT";
                  var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
                  var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
                  ContentDispositionHeaderValue contentDisposition = null;
                  if (ContentDispositionHeaderValue.TryParse("inline; filename=TData.json", out contentDisposition))
                  {
                      response.Content.Headers.ContentDisposition = contentDisposition;
                  }
                  return response;
              }
          }
          catch (Exception ex)
           {   var returnObj = new { error = ex.Message };
               var response = Request.CreateResponse(HttpStatusCode.BadRequest, returnObj, MediaTypeHeaderValue.Parse("application/json"));
               ContentDispositionHeaderValue contentDisposition = null;
               if (ContentDispositionHeaderValue.TryParse("inline; filename=error.json", out contentDisposition))
               {
                   response.Content.Headers.ContentDisposition = contentDisposition;
               }
               return response;
           }
       }
      }
  </code></pre>
  
  <p>And the API works perfectly for the valid Query parameters. But I am trying to implement error handling when there are any exception in the API it has to return a JSON like <code>{"error":" Exception Message"}</code>. In the above code I have the catch block to handle the exception. But it is not returning anything if there is any exception. Please suggest me a simple way of constructing JSON on exception. 
      <a href="https://i.stack.imgur.com/elxwA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/elxwA.jpg" alt="enter image description here"></a></p>