Author Topic: .net crashing, no error help, any ideas?  (Read 4651 times)

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
.net crashing, no error help, any ideas?
« on: April 19, 2010, 05:15:03 PM »
I have been messin' with .net for a while and I stumbled on a big problem. Basically I was playing around with drawing filled circles with custom rgb colors:

 .net detects all the typographical errors (like an unDIMentioned integer or something)  (am I using the right terminology?, anyway...)

That was all good until my rgb tried to except a number more then 255 (I was using the form's width as an input into the rgb function)
Instead of giving me an "overflow" error (or something similar) the whole d** program just crashed. Does .net do that for all mathematical errors? If so, it will be a lot of pain in the a** to program since I will have no idea where and what kind of error I am looking on.

(   maybe it was bc I was using the form_paint procedure?)
« Last Edit: April 19, 2010, 05:23:59 PM by Botsareus »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
.net crashing, no error help, any ideas?
« Reply #1 on: April 19, 2010, 06:28:43 PM »
.NET does exceptions instead of the error codes in VB6.  If your program doesn't handle the exceptions, it gets sent to the OS where it triggers a (tada!) "unhandled exception" error.  Which end users usually call a "crash" but to a programmer means a problem happened that they should have caught in their error handling code.

See this article.

Error handling is a whole topic in itself, since you can write "correct" code that becomes impossible to maintain because of all the try/catch blocks you have to put everywhere.  But basically you should architect your program so that:

1.  End users can't cause any exceptions (sanitize user input).  99% of the time, when a user plays with your program no exceptions should be thrown.  So basically, don't use exceptions for things you expect to happen.
2.  When an exception does occur, it's caught at least at the top level of your program so you can gracefully respond (eg: "a problem has occurred, saving sim...").  With .NET it's also possible to examine the exception to get a call stack complete with file names and line numbers.
3.  If anything at all unexpected happens, it throws an exception instead of trying to mask the problem and continuing (allows early detection of problems and easier debugging).

Another good article: clicky.
« Last Edit: April 19, 2010, 06:36:09 PM by Numsgil »

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
.net crashing, no error help, any ideas?
« Reply #2 on: April 20, 2010, 05:43:08 PM »
So it is like a modified ‘On error resume next’. What a pain!

Quote
System.OutOfMemoryException
System.NullReferenceException
Syste.InvalidCastException
Syste.ArrayTypeMismatchException
System.IndexOutOfRangeException
System.ArithmeticException
System.DevideByZeroException
System.OverFlowException

The main problem I see here is: I have no idea what is the exception for RGB getting a number more then 255? What if the program is just crashing on me and I have no idea what specific exception is happening, is there any way for .net to tell me what specific exception I am dealing with?

Offline peterb

  • Bot Destroyer
  • ***
  • Posts: 148
    • View Profile
.net crashing, no error help, any ideas?
« Reply #3 on: April 20, 2010, 07:07:16 PM »
Quote from: Botsareus
So it is like a modified ‘On error resume next’. What a pain!

Quote
System.OutOfMemoryException
System.NullReferenceException
Syste.InvalidCastException
Syste.ArrayTypeMismatchException
System.IndexOutOfRangeException
System.ArithmeticException
System.DevideByZeroException
System.OverFlowException

The main problem I see here is: I have no idea what is the exception for RGB getting a number more then 255? What if the program is just crashing on me and I have no idea what specific exception is happening, is there any way for .net to tell me what specific exception I am dealing with?

mybe the fasted way to solve your max 255 value is to perform
X and 255   'fastest

i dont know why you get over 255, but try to limit usuage of devisions  (devided by zero)
Other options are to set breakpoints or to print all the variables  on screen or in the debug field.
But basicly i think thats the art of programming to for see such events, and use clean logic routines as much as possible.

You can also turn on strong typing (so you have to declare everything) and so get less errors from mixing up variants integers doubles etc


Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
.net crashing, no error help, any ideas?
« Reply #4 on: April 20, 2010, 09:18:42 PM »
Quote from: Botsareus
So it is like a modified 'On error resume next'. What a pain!

More like 'On error goto blah'.

Quote
Quote
System.OutOfMemoryException
System.NullReferenceException
Syste.InvalidCastException
Syste.ArrayTypeMismatchException
System.IndexOutOfRangeException
System.ArithmeticException
System.DevideByZeroException
System.OverFlowException

The main problem I see here is: I have no idea what is the exception for RGB getting a number more then 255? What if the program is just crashing on me and I have no idea what specific exception is happening, is there any way for .net to tell me what specific exception I am dealing with?

All those exceptions inherit from System.Exception.  So you can do something like this to catch all possible exceptions (in C#, you'll need to convert to VB yourself):

Code: [Select]
try
{
  myStuff();
}
catch (Exception e)
{
  Console.WriteLine("{0} Exception caught.", e);
}

Note, though, that you'll want to be careful using this sort of general try-catch block anywhere but the main procedure.  It can easily mask problems or bugs with no clear indication of what the problem was or that there even was a problem.
« Last Edit: April 20, 2010, 09:22:01 PM by Numsgil »

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
.net crashing, no error help, any ideas?
« Reply #5 on: April 30, 2010, 10:54:25 AM »
ok, but:

What If I have 20 Complex lines of code and I have no idea which line is causing it.
Don't I have to wrap each line with this 'Try Catch' stuff?

P.S.

I am beginning to like C#, it is like a mesh of VB and C++, I think the first language I will learn in collage will be C++ and I already know VB so...
« Last Edit: April 30, 2010, 11:08:39 AM by Botsareus »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
.net crashing, no error help, any ideas?
« Reply #6 on: April 30, 2010, 12:38:12 PM »
Quote from: Botsareus
ok, but:

What If I have 20 Complex lines of code and I have no idea which line is causing it.
Don't I have to wrap each line with this 'Try Catch' stuff?

No.  Part of the information in the exception is a full call stack.  So it not only tells you the line it's crashing on but also the full call stack so if the crash is inside some math function called from all over your code you can track down what was calling it on up the call stack.

See This member list of System.Exception.  You can call exception.Message to get a message describing the error.  You can also call exception.StackTrace for a stack trace.

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
.net crashing, no error help, any ideas?
« Reply #7 on: April 30, 2010, 04:39:08 PM »
aha, TY