473,413 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Catching System.OverflowExceptions

....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 Garcia
Yahoo IM: mmmmojobootay
CS degree arriving in: July 2005

Nov 21 '05 #1
5 1286

"Andrea" <dr********@email.uophx.edu> wrote
....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

Nov 21 '05 #2
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" <dr********@email.uophx.edu>

...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 Garcia
Yahoo IM: mmmmojobootay
CS degree arriving in: July 2005

Nov 21 '05 #3
"Andrea" <dr********@email.uophx.edu> schrieb:
....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
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #4
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

"Larry Serflaten" <se*******@usinternet.com> wrote in message news:u%****************@TK2MSFTNGP11.phx.gbl...

"Andrea" <dr********@email.uophx.edu> wrote
...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

Nov 21 '05 #5
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

"Cor Ligthert" <no************@planet.nl> wrote in message news:ea**************@TK2MSFTNGP10.phx.gbl...
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" <dr********@email.uophx.edu>

...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 Garcia
Yahoo IM: mmmmojobootay
CS degree arriving in: July 2005

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rolf | last post by:
I understand a compilation error occurs when a method that throws no exceptions is the only code in a try block. What I don't understnad is why I can specify the catching of an Exception for a...
15
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
5
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here...
1
by: vlar | last post by:
01/30/2004 I have posted "Serious bug in ngen.exe" in this thread but so far (in .NET 1.1 SP1) this bug is not yet fixed !!! So I am repeating this post again: Ngen stops catching of derived...
1
by: Chris LaJoie | last post by:
Hi, I have a question regarding the catching of popups in a separate window. I just can't get it to work. My browser is extremely simple and is designed for a single purpose: to open a 'netlet'...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.