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 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
Also look at the documentation for the DivideByZeroException class
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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?
|
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 =...
|
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:...
| |
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'...
|
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...
|
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...
|
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:
...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
| | |