Home > other >  Exception is thrown in the "wrong line" (C#, Windows Form, VS2017)
Exception is thrown in the "wrong line" (C#, Windows Form, VS2017)

Time:09-26

I have a strange problem, which is probably normal, although I'd be grateful for some explanation.

I'm writing a C# Windows Form program and whenever my code runs into an exception, it is thrown in the line of the main Form's generated code, here: enter image description here

My question is, why is it? The exception above is thrown in a different line of my code and yet, it shows here. I debugged my code, because I had idea where the exception might occur and when I reached the suspicious line, the Visual Studio jumped to the line on the picture and throwed the exception.

It is really inconvenient as I can't tell where some of my exceptions are truly thrown and it slows the bug solving process.

Can someone explain why this might be and how to "solve" this?

CodePudding user response:

It's not the wrong line per se; you've invoked (or caused the invoking of) some method using reflection, that something has crashed with an unhandled exception and your main thread has then experienced an exception and brought it to your attention.

The main thread was busy doing application.run when this domino effect stopped it, so it's the "right line" for that thread, it's just that it's probably not the thread that was doing the work that crashed. Use the inner exception details to work out where the problem lies or give a try at Klaus' suggestion to turn on "break when thrown".

My guess is you've been doing something on a background thread (do you have any background workers anywhere? Do they access arrays in their DoWork?) and crashed it; it's a fairly common cause of these in winforms apps

  • Related