That's an example ApiController
[RequestExceptionFilter]
public class RequestController : ApiController
{
public IHttpActionResult Post([FromBody]Request RequestDTO)
{
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 when 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 syntax try-catchhere? What is an agreement?
In all the samples that I see on the Internet, people are simple throw the exception, but they do not seem to catch it.
(Of course, if I click Run, the application returns BadRequestas expected, but the question is, should I use try-catchor just leave the above code as such?)
source
share