1

I would like the form in C# to crash when something happens, like accessing an array/using a null variable.

I use C++ mainly, which usually crashes on the spot, so I know the problem immediately.

Since I am to C#, I mainly write my stuff within the function, and the callstack simply reveals the OnPaint function being called. So I've no idea where it is calling the exception.

Couldn't find any solution and manually looking for the problem is really taking me too much time.

Though it would be ideal for me to get these 2 solutions, either 1 would work.

  1. Getting it to crash instead of throwing exception!
  2. Some way for the Callstack to show me where I am, it only shows OnPaint, where my OnPaint function has like many lines, thus taking me forever to debug it.

p.s I understand that I should have created separate functions to be called within, but I wasn't sure how to do it in C# initially when I just wanted to create this program quickly for its purpose.

Thank you in advance!

yuletide
  • 124
  • 1
  • 12
  • Are you compiling with DEBUG turned on? This will give you line numbers in stack traces. – harpo Jul 13 '12 at 05:00
  • 1
    @harpo you could very well compile to RELEASE and still have line numbers. [Just ensure to have PDBs generated](http://i.imgur.com/rRIYW.png). – Uwe Keim Jul 13 '12 at 05:01
  • Funny how you want C# behave like C++. Do you code in notepad and compile in console? Try VisualStudio Express, it's free and it should have option 'Stop on exceptions'. It will point you right to the error. – alxx Jul 13 '12 at 05:33
  • To answer your question literally: you can wrap `Main()` in try/catch and program will just vanish after the crash. But it won't help you to debug it. – alxx Jul 13 '12 at 05:37
  • @UweKeim, I should have said `/debug`. – harpo Jul 13 '12 at 09:41

3 Answers3

3

Are you using Visual Studio?

If yes Go to Debug -> Exceptions and Check all the checkboxes in the dialog that opens

Now you would immediately break on error and at exact line...

Arif Eqbal
  • 3,068
  • 1
  • 18
  • 10
  • Yes, this helps so much! Someone please help to update this, I can't upvote this -_-, but this fixed the problem exactly. – ChangYou Wong Jul 13 '12 at 05:47
  • There should be a check mark under the vote count. Click that check mark to indicate this is the answer you needed. – Emily Jul 13 '12 at 05:52
1

Is there anything in the InnerException object on the Exception that's thrown?

It might be worth be handling the exception and outputting the value of any variables that are in memory at that point into a log file of some sort.

Here's an example on how to do it: How to Log Exception in a file?

Community
  • 1
  • 1
Fellmeister
  • 591
  • 3
  • 24
  • I put a break point at the exception to see what exactly happen, but still I didn't know where it is happening. Nevertheless the problem is fixed by Arif's solution – ChangYou Wong Jul 13 '12 at 05:51
0

If you are working inside a development environment, don't underestimate the power of breakpoints.

Emily
  • 444
  • 1
  • 3
  • 12