0

Anyhow i try when I throw exception in controller layer, this catch line does not fire.

Connecterd link Global exception handling in OWIN middleware

   public class GlobalExceptionMiddleware : OwinMiddleware
    {
        public GlobalExceptionMiddleware(OwinMiddleware next) : base(next)
        { }

        public override async Task Invoke(IOwinContext context)
        {
            try
            {
                await Next.Invoke(context);
            }
            catch (Exception ex)
            {

            }
        }
    }

I throw exception in this way:

public class MyController
    {
      public async Task<MyRestModel> Get(int id)
            {
                throw new Exception();

What I am doing wrong?

Jaczura Zoltán
  • 269
  • 3
  • 16
  • 1
    Please post more code, ideally a [MCVE]. There is all kinds of things that can go wrong. Are you sure your middleware is even hit? Did you set a breakpoint? – nvoigt Apr 11 '18 at 08:00
  • I completed the middleware code. Yes, I set a breakpoint at await Next.Invoke(context); and the code is stoped there. The controller throw the exception but the catch branch did not catch the exception. – Jaczura Zoltán Apr 11 '18 at 08:25
  • So *did* you set a breakpoint on the `await Next.Invoke(context);` line? Is it hit? – nvoigt Apr 11 '18 at 08:29
  • Yes. It is hit. – Jaczura Zoltán Apr 11 '18 at 08:34
  • as Dr Schizo pointed out in an comment of the top-answer of the question you linked you can not catch exceptions this way unfortunately – anion May 08 '19 at 12:26
  • the problem is in Owin engine which is swallowing all exceptions by self – Damir Beylkhanov Mar 18 '20 at 12:53

0 Answers0