WebAPI --> OWIN Middleware Exceptions RRS feed

  • Question

  • Greetings,

    When our WebApi Controller is

    • called by our OWIN middleware component, and
    • throws an Exception
    • the Exception is not caught in the OWIN middleware component

    Here is the WebApi Controller:

    public class CustomersController : ApiController


           public async Task<IHttpActionResult> Get()


                int xxx = 777;

                int y = 5 / xxx;  // throws System.DivideByZeroException



    Here is the Owin Middleware Component:

    public class GlobalExceptionHandler : OwinMiddleware


        public GlobalExceptionHandler(OwinMiddleware next, IAppBuilder app) : base(next)

        { }

        public override async Task Invoke(IOwinContext context)




              await Next.Invoke(context);  // calls WebApi Controller

              int x = 1;                   // The call returns here



           catch ( System.DivideByZeroException )


                    int x = 1;             // never executed


           catch (Exception ex)


                   int x = 1;              // never executed





    And here is the result in Postman:


      "Message": "An error has occurred.",

      "ExceptionMessage": "Attempted to divide by zero.",

      "ExceptionType": "System.DivideByZeroException",

      "StackTrace": " …

    Any explanation why the catch block is not catching the Exception?

    Conrad Krinock

    Thursday, May 11, 2017 1:07 PM

All replies

  • Hi Conrad Krinock,

    This forum discusses and asks questions about .NET Framework Base Classes, Since your issue is more related to webapi, I would suggest that you could post your issue on webapi forum for suitable support.

    Thanks for your understanding.

    Best regards,

    Cole Wu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, May 12, 2017 7:38 AM