I have a global exception handler in my web api project. This works great unless an exception is expressed through a lambda expression. I gave an example code below:
[HttpGet] public IHttpActionResult Test() { //Throw new Exception();// this exception is handled by my ExceptionHandler var list = new List<int>(); list.Add(1); IEnumerable<int> result = list.Select(a => GetData(a)); return Ok(result); } private static int GetData(int a) { throw new Exception();//This is not handled by my global exception handler }
This is my global exception handler.
public class ExceptionHandlerAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) {
I will register it in my WebApiConfig class
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { action = "Get", id = RouteParameter.Optional } ); config.Filters.Add(new ExceptionHandlerAttribute()); } }
source share