Catching System.OverflowExceptions

  • Thread starter Thread starter Andrea
  • Start date Start date
A

Andrea

....without changing the data type. I have been working on this for over 5 hours, please help me so I don't have to shoot myself. It's due by midnight...

Here's my code:

(Variables are declared as Short, therefore any sum over 32767 causes an overflow exception.)
If RadioButton1.Checked = True Then

Try

TextBox3.Text = FirstNum + SecondNum

Catch ex As OverflowException

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKOnly, "Maximum Value Exceeded")

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox1.Focus()

Return

End Try

End If

If RadioButton2.Checked = True Then

'Test for Overflow exception

Try

TextBox3.Text = FirstNum - SecondNum

Catch MyErr As ArithmeticException

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKOnly, "Maximum Value Exceeded")

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox1.Focus()

Return

End Try

End If

The problem is, it works for values a little bit over the range, but not if you put in 99999 plus 99999. I can't figure out why and how it is differentiating, if it isn't simply the limit of the Short data type.

Anyone that can help, I will buy you a drink!!!
 
....without changing the data type. I have been working on this for over 5 hours, please help me so I don't have to shoot myself.
It's due by midnight...

Here's my code:
(Variables are declared as Short, therefore any sum over 32767 causes an overflow exception.)
<...>
The problem is, it works for values a little bit over the range, but not if you put in 99999 plus 99999. I can't figure out why
and how it is differentiating, if it isn't simply the limit of the Short data type.

Anyone that can help, I will buy you a drink!!!


Two points, keep data types separated, and avoid redundant code.

When adding numbers, the result should go into another variable of the
appropreate type, not into a string, or string property. Adding unecessary
code is unecessary, and code that is not necessary should not be included.
A more concise version would look like:

Try
If RadioButton1.Checked = True Then
total = FirstNum + SecondNum
Else
total = FirstNum - SecondNum
End If
TextBox3.Text = CStr(Total)
Return
Catch ov As OverflowException
MsgBox("The result of the calculation is out of range.", MsgBoxStyle.OKOnly, "Data Type Range Exceeded")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Calculation Error")
End Try

TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox1.Focus()
Return


Finally, you do not show where FirstNum and SecondNum are assigned.
If you input 99999 then it can't fit in a Short type so in that case, the code
would error before it gets this far. (It would error where you try to put 99999
into a Short type.)

LFS
 
Andrea,

There is a standard answer for this.
Set option Strict to on in your IDE options (as I do) or set it in top of every file.

You will see than probably all the problems in the same way as Larry described them.

I hope tis helps?

Cor
"Andrea" <[email protected]>

...without changing the data type. I have been working on this for over 5 hours, please help me so I don't have to shoot myself. It's due by midnight...

Here's my code:

(Variables are declared as Short, therefore any sum over 32767 causes an overflow exception.)
If RadioButton1.Checked = True Then

Try

TextBox3.Text = FirstNum + SecondNum

Catch ex As OverflowException

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKOnly, "Maximum Value Exceeded")

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox1.Focus()

Return

End Try

End If

If RadioButton2.Checked = True Then

'Test for Overflow exception

Try

TextBox3.Text = FirstNum - SecondNum

Catch MyErr As ArithmeticException

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKOnly, "Maximum Value Exceeded")

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox1.Focus()

Return

End Try

End If

The problem is, it works for values a little bit over the range, but not if you put in 99999 plus 99999. I can't figure out why and how it is differentiating, if it isn't simply the limit of the Short data type.

Anyone that can help, I will buy you a drink!!!
 
Andrea said:
over 5 hours, please help me so I don't have to shoot myself.
It's due by midnight...
:-(((

Here's my code:

(Variables are declared as Short, therefore any sum over 32767 causes
an overflow exception.)
If RadioButton1.Checked = True Then

Try

TextBox3.Text = FirstNum + SecondNum

Turn 'Option Strict On'.

\\\
TextBox3.Text = CStr(FirstNum + SecondNum)
///
Catch ex As OverflowException

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.",
MsgBoxStyle.OKOnly, >"Maximum Value Exceeded")
TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox1.Focus()

Return

End Try

End If
[...]
The problem is, it works for values a little bit over the range,
but not if you put in 99999 plus 99999. I can't figure out why
and how it is differentiating, if it isn't simply the limit of the
Short data type.

How are you filling 'FirstNum' and 'SecondNum'? If these variables are
declared as 'Short', 99,999 cannot be stored in them because it exceeds
'Short.MaxValue'.
 
thanks, i will take a close look at this, and try your code...

FirstNum and SecondNum were declared at the very beginning, I guess I should have included that part too. They are declared as Short, but our instructor wanted us to figure out how to handle the errors without changing the data type.

--
Andrea Garcia
Yahoo IM: mmmmojobootay
CS degree arriving in: July 2005


...without changing the data type. I have been working on this for over 5 hours, please help me so I don't have to shoot myself.
It's due by midnight...

Here's my code:
(Variables are declared as Short, therefore any sum over 32767 causes an overflow exception.)
<...>
The problem is, it works for values a little bit over the range, but not if you put in 99999 plus 99999. I can't figure out why
and how it is differentiating, if it isn't simply the limit of the Short data type.

Anyone that can help, I will buy you a drink!!!


Two points, keep data types separated, and avoid redundant code.

When adding numbers, the result should go into another variable of the
appropreate type, not into a string, or string property. Adding unecessary
code is unecessary, and code that is not necessary should not be included.
A more concise version would look like:

Try
If RadioButton1.Checked = True Then
total = FirstNum + SecondNum
Else
total = FirstNum - SecondNum
End If
TextBox3.Text = CStr(Total)
Return
Catch ov As OverflowException
MsgBox("The result of the calculation is out of range.", MsgBoxStyle.OKOnly, "Data Type Range Exceeded")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Calculation Error")
End Try

TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox1.Focus()
Return


Finally, you do not show where FirstNum and SecondNum are assigned.
If you input 99999 then it can't fit in a Short type so in that case, the code
would error before it gets this far. (It would error where you try to put 99999
into a Short type.)

LFS
 
thank you, I never even thought about changing option strict. I think I have it set as default in the IDE, but I can check.

--
Andrea Garcia
Yahoo IM: mmmmojobootay
CS degree arriving in: July 2005

Andrea,

There is a standard answer for this.
Set option Strict to on in your IDE options (as I do) or set it in top of every file.

You will see than probably all the problems in the same way as Larry described them.

I hope tis helps?

Cor
"Andrea" <[email protected]>

...without changing the data type. I have been working on this for over 5 hours, please help me so I don't have to shoot myself. It's due by midnight...

Here's my code:

(Variables are declared as Short, therefore any sum over 32767 causes an overflow exception.)
If RadioButton1.Checked = True Then

Try

TextBox3.Text = FirstNum + SecondNum

Catch ex As OverflowException

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKOnly, "Maximum Value Exceeded")

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox1.Focus()

Return

End Try

End If

If RadioButton2.Checked = True Then

'Test for Overflow exception

Try

TextBox3.Text = FirstNum - SecondNum

Catch MyErr As ArithmeticException

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKOnly, "Maximum Value Exceeded")

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox1.Focus()

Return

End Try

End If

The problem is, it works for values a little bit over the range, but not if you put in 99999 plus 99999. I can't figure out why and how it is differentiating, if it isn't simply the limit of the Short data type.

Anyone that can help, I will buy you a drink!!!
 

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

Similar Threads

Create a public variable 4
parsing 1
Web Database (Insert) 1
ASP.NET Proyect 2
SetFocus problem 7
Catch a wrong value 8
C# Databinding Issue 0
.ExecuteNonQuery() 2

Back
Top