title: Using ExceptionFilterAttribute in Web API
body: <p>I am trying to implement the error handling in the created Web API, need to return the exception details in JSON format. I created the BALExceptionFilterAttribute like </p>
  
  <pre><code>public class BALExceptionFilterAttribute : ExceptionFilterAttribute
  {
      public override void OnException(HttpActionExecutedContext actionExecutedContext)
      {
          base.OnException(actionExecutedContext);
          actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, new { error = actionExecutedContext.Exception.Message });
      }
  }
  </code></pre>
  
  <p>And registered them in Gloal.asax.cs like</p>
  
  <pre><code>GlobalConfiguration.Configuration.Filters.Add(new BALExceptionFilterAttribute());
  </code></pre>
  
  <p>In my Controllers I want to throw the exception like</p>
  
  <pre><code>    [HttpGet]
      [BALExceptionFilter]
      public HttpResponseMessage Getdetails(string ROOM, DateTime DOB_GT)
      {
          if (string.IsNullOrEmpty(ROOM) 
          {
              return Request.CreateResponse(new { error = "Input paramete cannot be Empty or NULL" });
          }
              //throws the exception
              throw new BALExceptionFilterAttribute();
  
              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["TGSDataBaseConnection"].ConnectionString;
              using (OracleConnection dbconn = new OracleConnection(connStr))
              {
                  DataSet userDataset = new DataSet();
                  var strQuery = "SELECT * from LIMS_SAMPLE_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=TGSData.json", out contentDisposition))
                  {
                      response.Content.Headers.ContentDisposition = contentDisposition;
                  }
                  return response;
                 }
          }
  </code></pre>
  
  <p>But it shows error like  on the <code>throw new BALExceptionFilterAttribute();</code>
  <code>Error  1   The type caught or thrown must be derived from System.Exception</code></p>