473,668 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Catching System.Overflow Exceptions

....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.Ch ecked = True Then

Try

TextBox3.Text = FirstNum + SecondNum

Catch ex As OverflowExcepti on

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

TextBox1.Clear( )

TextBox2.Clear( )

TextBox3.Clear( )

TextBox1.Focus( )

Return

End Try

End If

If RadioButton2.Ch ecked = True Then

'Test for Overflow exception

Try

TextBox3.Text = FirstNum - SecondNum

Catch MyErr As ArithmeticExcep tion

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKO nly, "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 1310

"Andrea" <dr********@ema il.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.Ch ecked = True Then
total = FirstNum + SecondNum
Else
total = FirstNum - SecondNum
End If
TextBox3.Text = CStr(Total)
Return
Catch ov As OverflowExcepti on
MsgBox("The result of the calculation is out of range.", MsgBoxStyle.OKO nly, "Data Type Range Exceeded")
Catch ex As Exception
MsgBox(ex.Messa ge, MsgBoxStyle.OKO nly, "Calculatio n 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********@ema il.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.Ch ecked = True Then

Try

TextBox3.Text = FirstNum + SecondNum

Catch ex As OverflowExcepti on

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

TextBox1.Clear( )

TextBox2.Clear( )

TextBox3.Clear( )

TextBox1.Focus( )

Return

End Try

End If

If RadioButton2.Ch ecked = True Then

'Test for Overflow exception

Try

TextBox3.Text = FirstNum - SecondNum

Catch MyErr As ArithmeticExcep tion

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKO nly, "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********@ema il.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.Ch ecked = True Then

Try

TextBox3.Tex t = FirstNum + SecondNum
Turn 'Option Strict On'.

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

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKO nly, >"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*******@usin ternet.com> wrote in message news:u%******** ********@TK2MSF TNGP11.phx.gbl. ..

"Andrea" <dr********@ema il.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.Ch ecked = True Then
total = FirstNum + SecondNum
Else
total = FirstNum - SecondNum
End If
TextBox3.Text = CStr(Total)
Return
Catch ov As OverflowExcepti on
MsgBox("The result of the calculation is out of range.", MsgBoxStyle.OKO nly, "Data Type Range Exceeded")
Catch ex As Exception
MsgBox(ex.Messa ge, MsgBoxStyle.OKO nly, "Calculatio n 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******** ******@TK2MSFTN GP10.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********@ema il.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.Ch ecked = True Then

Try

TextBox3.Text = FirstNum + SecondNum

Catch ex As OverflowExcepti on

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

TextBox1.Clear( )

TextBox2.Clear( )

TextBox3.Clear( )

TextBox1.Focus( )

Return

End Try

End If

If RadioButton2.Ch ecked = True Then

'Test for Overflow exception

Try

TextBox3.Text = FirstNum - SecondNum

Catch MyErr As ArithmeticExcep tion

MsgBox("Sum of Variable 1 and Variable 2 is greater than maximum allowed.", MsgBoxStyle.OKO nly, "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
2972
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 method that throws no exceptions, but I cannot catch an IOException for a method that throws no exceptions? // This try/catch code complies fine, even though foo2 throws no exceptions:
15
18009
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 (SEH) in C++ EH so that a catch (...) will catch an access violation. I don't know if other platforms support something similar. I'm wondering about how to best protect an application or library from poorly written user-defined callbacks. It...
0
2719
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 violations aren't part of the standard C++ >> exception handling support. On Windows, a particular MSVC compiler >> option enables Microsoft's Structured Exception Handling (SEH) in C++ >> EH so that a catch (...) will catch an access violation. ...
5
2254
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 is to ensure that no child window can close while it is in a state where user entered information can be lost. I have just noticed that while the Closing event is caught if I click the X on the child window, it is not caught if I click the X on...
1
1522
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 exception in multi dll application ! I have reproduced this bug in the very simple application which consists of 2 dll and 1 exe file. Its behaviour is different depending on whether it is "ngen"-ed or not: <<<ClassLibrary1.dll>>>
1
7831
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' to a particular VPN on the United Airlines network. The trouble is, I can't get my app to catch the popup (which is the netlet). The netlet opens outside in a separate IE window instead. I have a VB6 app that does what I want perfectly fine...
7
2332
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 of code, when System.Exception seems to get the job done for me. My application is an ASP.Net intranet site. When I catch an exception, I log the stack trace and deal with it, normally by displaying an error
12
6103
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 out of which unmanaged exception //get thrown
2
17006
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 the columns of the data grid contains a DropDownlist. I managed to create this datagrid control as follows.
2
2352
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
8462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8797
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8583
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8656
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7401
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.