3/0 -> doesn't throw DivideByZeroException ? 
Author Message
 3/0 -> doesn't throw DivideByZeroException ?

If you try this in VB.Net

Try
    messagebox.show("Test" & 3/0)
Catch E as DivideByZeroException
    messagebox.show ("div 0")
Catch E as Exception
    messagebox.show(E.message)
end try

you get an 'overflow exception' !!

does anyone have an idea about that ? (i know that single and double reports
Nan/+inf/-inf , i tested it with Byte, Short, Integer, Long ...)



Mon, 27 Sep 2004 15:02:16 GMT  
 3/0 -> doesn't throw DivideByZeroException ?

Quote:
>does anyone have an idea about that ? (i know that single and double reports
>Nan/+inf/-inf , i tested it with Byte, Short, Integer, Long ...)

/ is VB's floating point division operator. The operands are
implicitly converted to a floating point type before the division is
performed.

If you want integer division, use the \ operator.

Mattias

===
Mattias Sj?gren (VB MVP)

http://www.msjogren.net/dotnet/



Mon, 27 Sep 2004 15:57:23 GMT  
 3/0 -> doesn't throw DivideByZeroException ?
That is because x/0 produces infinity, or undefined.


Mon, 27 Sep 2004 17:21:18 GMT  
 3/0 -> doesn't throw DivideByZeroException ?
hi mattias,

i already knew that, but what i mean is that it's strange because this code
:

Dim a,b,c As Integer
Try
a = 3
b = 0
c = a / b
Catch e As Exception
MessageBox.Show(e.Message)
End Try

throw an exception and display an 'overflow error' message.

but if you define a, b and c as double , it doesn't throw any exception (i
agree with this, because the result is 1.#inf it's not considered as an
exception)

i think that a and b are implicitly converted to double by the / operator,
but the result of this operation is 1.#inf which is not convertible to
Integer, that's why i have an overflow error ... but i maintain that it
should throw a DivideByZeroException (it would be more logical ...)


Quote:

> >does anyone have an idea about that ? (i know that single and double
reports
> >Nan/+inf/-inf , i tested it with Byte, Short, Integer, Long ...)

> / is VB's floating point division operator. The operands are
> implicitly converted to a floating point type before the division is
> performed.

> If you want integer division, use the \ operator.

> Mattias

> ===
> Mattias Sj?gren (VB MVP)

> http://www.msjogren.net/dotnet/



Mon, 27 Sep 2004 22:56:59 GMT  
 3/0 -> doesn't throw DivideByZeroException ?

<cut>

Quote:
> i think that a and b are implicitly converted to double by the /
> operator, but the result of this operation is 1.#inf which is not
> convertible to Integer, that's why i have an overflow error ... but i
> maintain that it should throw a DivideByZeroException (it would be
> more logical ...)

No, you are putting 'infinity' into an Integer and that's an overflow.  The
division by zero operation is already over and done with.  What would you
expect in a case like this:

Dim a,b,c As Integer
Dim d As Double
Try
a = 3
b = 0
d = a / b
c = d
Catch e As Exception
MessageBox.Show(e.Message)
End Try

BTW, with Option Strict On and use of the Convert.ToInt32 you get an error
that the "Value was either too large or too small" rather than an overflow
exception.



Mon, 27 Sep 2004 23:46:42 GMT  
 3/0 -> doesn't throw DivideByZeroException ?
Overflow error on 'c=d' ...

that's what i mean in my previous posts... i know that arithmetic operations
on double and single doesn't throw dividebyzero exception ... but for
integers it should ! (the .Net framework doc says that if you divide an
integer by zero it throws a dividebyzeroexception...) otherwise why does the
dividebyzeroexception exists ? (except that we can throw it ourself...)


Quote:


> <cut>
> > i think that a and b are implicitly converted to double by the /
> > operator, but the result of this operation is 1.#inf which is not
> > convertible to Integer, that's why i have an overflow error ... but i
> > maintain that it should throw a DivideByZeroException (it would be
> > more logical ...)

> No, you are putting 'infinity' into an Integer and that's an overflow.
The
> division by zero operation is already over and done with.  What would you
> expect in a case like this:

> Dim a,b,c As Integer
> Dim d As Double
> Try
> a = 3
> b = 0
> d = a / b
> c = d
> Catch e As Exception
> MessageBox.Show(e.Message)
> End Try

> BTW, with Option Strict On and use of the Convert.ToInt32 you get an error
> that the "Value was either too large or too small" rather than an overflow
> exception.



Tue, 28 Sep 2004 15:36:55 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Query criteria >50 doesn't work if passed from form..;(

2. fRefreshLinks Doesn't work if path doesn't exist

3. Form_Load() doesn't execute, form doesn't load

4. class doesn't support automation or doesn't support expected interface

5. asyncread doesn't tell if file doesn't exists

6. new user-->>>How can i use existing exe's in vb

7. using The Shell Command >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

8. <<<<<<<<ComboBox>>>>>>>>>>>>

9. >>>>>>While Not rst.EOF

10. PSUDOCODE HELP >>>>>>>>>>>

11. 'Next Comment' doesn't work

12. Wanted >>QBasic Tips For Notebook Page on Kenneth Green's Homepage

 

 
Powered by phpBB® Forum Software