473,473 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Division by Zero, Exception or Not?

Sometimes, when I think I understand something and try to explain it to
someone else, I realize that I don't understand it at all.

Consider:

Dim i As Integer
Dim d As Double
Dim a As Integer = 3
Dim b As Integer = 0

' case 1
Try
TextBox1.Text = a / b
Debug.WriteLine(i)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

' case 2
Try
TextBox1.Text = a \ b
Debug.WriteLine(d)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

' case 3
Try
i = a / b
Debug.WriteLine(i)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

' case 4
Try
d = a \ b
Debug.WriteLine(d)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

case 1 puts "Infinity" in TextBox1 but the other three throw an
exception and put "Uh Oh" there instead. This makes no sense to me. At
first thought, I would expect a div 0 error to ALWAYS result in an
exception as in 2, 3, and 4.

But then, maybe there's special behavior since VB "knows" that the
assignment is to a text box and so it gives a "friendly" answer for 1,
but why doesn't it do that for 2?

This seems VERY inconsistent behavior. If not, why?

Sean
Nov 21 '05 #1
3 2297
Try to do the same thingy with option strict on and there is your answer :-)

regards

Michel Posseth

"Sean Kirkpatrick" <na**********@community.nospam> wrote in message
news:uZ**************@TK2MSFTNGP15.phx.gbl...
Sometimes, when I think I understand something and try to explain it to
someone else, I realize that I don't understand it at all.

Consider:

Dim i As Integer
Dim d As Double
Dim a As Integer = 3
Dim b As Integer = 0

' case 1
Try
TextBox1.Text = a / b
Debug.WriteLine(i)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

' case 2
Try
TextBox1.Text = a \ b
Debug.WriteLine(d)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

' case 3
Try
i = a / b
Debug.WriteLine(i)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

' case 4
Try
d = a \ b
Debug.WriteLine(d)
Catch ex As Exception
TextBox1.Text = "Uh Oh"
End Try

case 1 puts "Infinity" in TextBox1 but the other three throw an exception
and put "Uh Oh" there instead. This makes no sense to me. At first
thought, I would expect a div 0 error to ALWAYS result in an exception as
in 2, 3, and 4.

But then, maybe there's special behavior since VB "knows" that the
assignment is to a text box and so it gives a "friendly" answer for 1, but
why doesn't it do that for 2?

This seems VERY inconsistent behavior. If not, why?

Sean

Nov 21 '05 #2
Also look at the documentation for the DivideByZeroException class

Nov 21 '05 #3

You have the operators confused.

\ is the Integral division
/ the floating point
a / b, gives infinity
a \ b, gives a div-zero error
case 1

a / b -> Infiinty Infinity.ToString() = "Infinity"
Case 2

a \ b DIVZERO
case 3

int i = a/b ; tryng to assign Infinity to integer - error
case 4

double d = a \b integral div by 0 error

NOTES:

You should turn on Option Strict
It will flag case 3 as an invalid assignment.
hth
Alan

Nov 21 '05 #4

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

Similar topics

20
by: BigMan | last post by:
On most architectures integer division by 0 results in throwing an exception (does anyone know of one that behaves differently?). I need a way to catch FP division by 0. Is there a standard way to...
1
by: florian.boldt | last post by:
Hi Folks, one of our developers uses a statement with a where clause which usually does not match to any rows. In case of one or more rows found she wrote a an expression in the select clause...
6
by: bart van deun | last post by:
Hello, i have a problem with a division by zero fault in my report trough a query... this is the expression in the query: Name: Sum(IIF(=0,0,/)) what is wrong with this expression?
9
by: Marcin | last post by:
How I can make division of two numbers placed in arrays, example: short int a = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2}; short int b =...
2
by: subnunciation | last post by:
i know, this shouldnt be a conundrum right? one just shouldnt divide by zero. but this is suddenly happening *all over* my site. after chasing the error here and there, i simplified things down to:...
2
by: Pakna | last post by:
Why do I have a zero result on this query for all non-zero rows? Do I have to declare specifically the precision of ratio division? How do I do that? And is there a way to make DB2 'ignore'...
0
by: LJ72 | last post by:
Hi I have taken over someone elses work and dont know VB at all. I am trying to work out percentage changes between two quarters data and am using the following formula. 'Box 4072 Vet...
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
9
by: Joe Cool | last post by:
Hello, I am using VS2005. I am trying to convert a VB.NET app to C#.NET. The VB app uses a progress meter to indiate how far a file has been read. I use the following assignment statement in VB: ...
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
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...
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.