Exceptions are not raised

  • Thread starter Giuseppe Chielli
  • Start date
G

Giuseppe Chielli

Hi to everyone. I am studying VB.net and I am wondering why this trivial
code doesn't raise any exception:

Option Strict On

Imports System
Imports Microsoft.VisualBasic


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim num1, num2 As Short
Dim num3 As Double
num1 = 5
num2 = 0
Try
num3 = num1 / num2
Catch ex As DivideByZeroException
MsgBox("Divisione per 0!")
End Try
End Sub

End Class

No msgbox is executed!!!
I'm using VB 2005 Express Edition.
Should I set anything in the framework or project properties?
Thanks in advance to everyone.

Giuseppe from Italy.
 
K

Kevin Spencer

Exceptions certainly *are* raised when they occur. However, in this case,
you're expecting a "divide by zero" exception, but it will not occur,
because in the .Net Platform, dividing by zero is not an illegal operation.
It results in "infinity."

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
G

Guest

Hi to everyone. I am studying VB.net and I am wondering why this trivial
code doesn't raise any exception:
Dim num1, num2 As Short
Dim num3 As Double
num1 = 5
num2 = 0
Try
num3 = num1 / num2
Catch ex As DivideByZeroException
MsgBox("Divisione per 0!")
End Try

Divide by zero is for integral types. Change / to \ and you should get an
exception. When you use /, integral num1 and num2 are cast to floating
point, and since num3 is double, no exception. Cint(num1 / num2) would also
generate an exception (not divide by zero, don't remember exactly what).
 
R

R. MacDonald

Hello, Giuseppe,

To expand a little on the previous posts, .Net sets the floating point
control register in such a way that this operation, and a host of
others, do not signal an error. Instead of an error, a "special"
floating point value is returned. The possible return values (for
Double) are:

Double.NaN()
Double.NegativeInfinity()
Double.PositiveInfinity()

You can test for these results by using the functions:

Double.IsInfinity()
Double.IsNaN()
Double.IsNegativeInfinity()
Double.IsPositiveInfinity()

(There are corresponding values and functions for Single.)

I don't know if the floating point control register can be controlled
directly in .Net, but other languages allow this. So it IS possible to
change the behaviour so that floating point errors will raise exceptions
by calling an unmanaged routine. However (since other .Net code will
not be expecting it) it is probably not a good idea in general. If you
were to do this however, I think that the exception raised will be
ArithmeticException, not DivideByZeroException, which (as was pointed
out) is for Integral data types.

Cheers,
Randy
 
G

Giuseppe Chielli

R. MacDonald said:
Hello, Giuseppe,

To expand a little on the previous posts, .Net sets the floating point
control register in such a way that this operation, and a host of
others, do not signal an error. Instead of an error, a "special"
floating point value is returned. The possible return values (for
Double) are:

Double.NaN()
Double.NegativeInfinity()
Double.PositiveInfinity()

You can test for these results by using the functions:

Double.IsInfinity()
Double.IsNaN()
Double.IsNegativeInfinity()
Double.IsPositiveInfinity()

(There are corresponding values and functions for Single.)

I don't know if the floating point control register can be controlled
directly in .Net, but other languages allow this. So it IS possible to
change the behaviour so that floating point errors will raise exceptions
by calling an unmanaged routine. However (since other .Net code will
not be expecting it) it is probably not a good idea in general. If you
were to do this however, I think that the exception raised will be
ArithmeticException, not DivideByZeroException, which (as was pointed
out) is for Integral data types.
Cheers,
Randy
Thanks a lot for you explanation.That's very useful for me!!!
Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top