C# Overflow error! Any help?

C

Chua Wen Ching

Thanks Derek...

Okay i had another question..

my program runs smoothly for the first minute, after 1
minute...

suddenly it breaks and display this error:

do you know what is the cause of this problem...

i had check my codes and confirm it is working.. shouldn't
be a problem on logic side

An unhandled exception of type 'System.OverflowException'
occurred in system.windows.forms.dll
Additional information: Overflow error.

Any help?

Regards,
Chua Wen Ching :p
 
J

Jon Skeet

Chua Wen Ching said:
Okay i had another question..

my program runs smoothly for the first minute, after 1
minute...

suddenly it breaks and display this error:

do you know what is the cause of this problem...

i had check my codes and confirm it is working.. shouldn't
be a problem on logic side

An unhandled exception of type 'System.OverflowException'
occurred in system.windows.forms.dll
Additional information: Overflow error.

Any help?

Without you posting some code, it's almost impossible to say. I suggest
you pare down your code to a short but complete example which
demonstrates the problem - see
http://www.pobox.com/~skeet/csharp/complete.html for what I mean. You
may well find the problem just by going through this exercise, but if
not, posting that code will help immensely.
 
J

Justin

This usually happens when you have a recursive function
call in your code that never ends and uses up all the
memory.

I had the problem once when I accidentally did this:

private bool member = false;

public bool Member
{
get
{
return Member; // instead of return member
}
}

check for that sort of situation
-----Original Message-----
Thanks Derek...

Okay i had another question..

my program runs smoothly for the first minute, after 1
minute...

suddenly it breaks and display this error:

do you know what is the cause of this problem...

i had check my codes and confirm it is working.. shouldn't
be a problem on logic side

An unhandled exception of
type 'System.OverflowException'
 
J

Jack Hanebach

An unhandled exception of type 'System.OverflowException'
occurred in system.windows.forms.dll
Additional information: Overflow error.

This is an exception thrown when the arithmetic operation results in
overfow. for instance:

byte b = 255;
b += 3; // overflow!
 
A

Ann Marinas

By default, C# will not check arithmetic for overflow.
Using this code as an example:

//example.cs
class Example
{
static void Main()
{
int number = int.MaxValue;
Console.Writeline(++number)
}
}

Suppose: number == -2147483648 ----> It will not get any error message.

If you're getting the System.OverflowException, your arithmetic overflow checking is turned on ( Ex.: c:\ csc /checked+ example.cs).

Solution 1 : Try turning it off .
Example:
c:\ csc /checked- example.cs -- will wrap the int value to int.MinValue and not cause the System.Overflow Exception.

Solution 2: Use Uncheck Statement

//example.cs
class Example
{
static void Main()
{
unchecked ---> all statements are never checked for arithmetic overflow
{
int number = int.MaxValue;
Console.Writeline(++number)
}
}
}

Solution 3: use unchecked statement

//example.cs
class Example
{
static void Main()
{
int number = int.MaxValue;
Console.Writeline(unchecked(++number)); ---> expression is checked for aruthmetic overflow
}
}


Hope this helps! :D

===================
A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top