This is a sample ApiController
used
[RequestExceptionFilter]
public class RequestController : ApiController
{
public IHttpActionResult Post([FromBody]Request RequestDTO)
{
//some code over here
throw new DTONullException(typeof(Request.Models.DTO.Request));
This is my custom exception handler
public class RequestExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is DTONullException)
{
context.Response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("DTO is null"),
ReasonPhrase = "DTO is null",
};
}
base.OnException(context);
}
}
And while debugging, I get this error:
An exception of type 'Request.Controllers.DTONullException' occurred in Request.dll but was not handled in user code
Should I use the try-catch
syntax here ? What is the convention ?
In all the samples I see in the internet, people just throw the exception
but they dont seem to catch it.
(Ofcourse, If i press Run
, the application returns a BadRequest
as expected but the question is should i use try-catch
or just leave the code above as such ?)