A question about NotFiniteNumberException

B

Brent Hill

Hello All!

I just have a quick little question that I have not been
able to solve myself. Within a program being developed in
VS.NET, I have written a private function with the
following code:


Private Function FmaxTest()
Dim varianceHi, varianceLo, Fmax As Double
........
(In this part of the code, the values of varianceHi and
varianceLo are established; depending on the dataset, it
is possible that varianceLo = 0 in some scenarios.)
........
Try
Fmax = varianceHi / varianceLo
Catch ex As NotFiniteNumberException
MsgBox("Division by zero not allowed.")
Exit Function
End Try
........
(This part of the code is for reporting the values of
varianceHi, varianceLo, and Fmax.)
........
End Function


I originally tried the DivideByZeroException, but
discovered that it was for use only with integers. I then
tried NotFiniteNumberException because (as I understand
it) it will throw an exception when there is +infinity, -
infinity, or NaN.

However, in such cases where varianceLo = 0, there are no
exceptions thrown, the algorithm continues as normal, and
the value of Fmax is reported to be Infinity. Any ideas
about what I am doing wrong? Thanks in advance for any
ideas.
 
R

Rob Oldfield

Haven't got a clue about the problem, but in practical terms why not just
use a...

If varianceLo = 0 Then

....somewhere?

Sorry if that's not esoteric enough.
 
K

Ken Tucker [MVP]

Hi,

The / operation will not throw an exception when you divide by zero.
Take a look at double.ispositiveinfinity, double.isnegativeinfinity, and
double.isnan

If Double.IsPositiveInfinity(1.1 / 0.0) Then

MessageBox.Show("Divide by zero")

End If



Ken
 
G

Guest

That is what I have been using for this particular type of
exception. However, I was thinking that it would be much
more practical to enclose all my calculations in a single
Try..Catch...Finally statement rather than create an
If...Then statement for each of the *many* calculations to
be performed. I would have to create many If...Then
statements because very few of the calculations being
performed share the same denominator term.

So, esoterica is not my concern, only pragmatics. But
thanks for trying anyway!
 
A

Armin Zingler

Ken Tucker said:
Hi,

The / operation will not throw an exception when you divide
by zero.
Take a look at double.ispositiveinfinity, double.isnegativeinfinity,
and double.isnan

If Double.IsPositiveInfinity(1.1 / 0.0) Then

MessageBox.Show("Divide by zero")

End If

Is this also valid?
if (1.1/0.0) = double.positiveinfinity then

It works, so I think it is, but I'm not 100% sure.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 

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