A question about exceptions

E

Edward

Hi everybody,
I was testing a piece of code and I noticed that try catch block doesn't
recognize "DivideByZeroException" when I have zero in denominator and I have
to use the "OverflowException" , I can't figure out why VB behaves this way ,
Any thoughts?
 
T

Tom Shelton

Hi everybody,
I was testing a piece of code and I noticed that try catch block doesn't
recognize "DivideByZeroException" when I have zero in denominator and I have
to use the "OverflowException" , I can't figure out why VB behaves this way ,
Any thoughts?

You probably need to read the documentation on the DivideByZeroException
:) See, this one is only thrown by integer and decimal types. Floating
point numbers, single and double do not throw this exception - they
return NaN.

Now, you are probably going to say that you are using integers - but,
probably not... See VB (both classic and .NET) have two division
operators / and \. The commonly used / actually performs it's work by
converting it's operands to double values - so it won't throw this
exception. I would bet that if you switched to \ instead - doing
integer division that it would throw a DivideByZeroException... Here
let me try it real quick :)

Yep... In fact, if you do say 3\0 using literals - it won't even
compile :) But if you do it with variables, then a
DivideByZeroException is thrown at runtime.
 
A

Armin Zingler

Edward said:
Hi everybody,
I was testing a piece of code and I noticed that try catch block
doesn't recognize "DivideByZeroException" when I have zero in
denominator and I have to use the "OverflowException" , I can't
figure out why VB behaves this way , Any thoughts?

Can't repro this. Can you show some code? Do you have Option Strict
enabled? I guess you try to assign the result of a floating point
division to an Integer value. So, not the division is the problem, the
assignment is. We must distinguish between floating point division and
integer division. The former does not throw an exception if the
demoninator is 0. Instead the result is PositiveInfinity,
NegativeInfinity or NaN (see help for / operator). The latter throws a
DivideByZeroException.


Armin
 
T

Tom Shelton

Can't repro this. Can you show some code? Do you have Option Strict
enabled? I guess you try to assign the result of a floating point
division to an Integer value. So, not the division is the problem, the
assignment is. We must distinguish between floating point division and
integer division. The former does not throw an exception if the
demoninator is 0. Instead the result is PositiveInfinity,
NegativeInfinity or NaN (see help for / operator). The latter throws a
DivideByZeroException.


Armin

/ converts its operands to floating point values - so does not throw a
DivisionByZeroException. You need to be doing integer division (\) to
get that exception.
 
K

Kerry Moorman

Tom,

I think it depends on the operand types. For example, this function will
throw a DivideByZero exception if donors is zero:

Private Function getAverage(ByVal donations As Decimal, ByVal donors As
Integer) As Decimal

'Calculate and return the average donation
getAverage = donations / donors

End Function

Kerry Moorman
 
A

Armin Zingler

Tom Shelton said:
/ converts its operands to floating point values - so does not throw
a DivisionByZeroException. You need to be doing integer division
(\) to get that exception.

Yep, that's what I wrote.


Armin
 
T

Tom Shelton

Tom,

I think it depends on the operand types. For example, this function will
throw a DivideByZero exception if donors is zero:

    Private Function getAverage(ByVal donations As Decimal, ByVal donors As
Integer) As Decimal

        'Calculate and return the average donation
        getAverage = donations / donors

    End Function

Kerry Moorman

Hmm... Actually, that's probably correct in this case. Since
decimals can hold a bigger range of numbers, the / operator probably
does use decimal in this case - and decimal is really an integer type
under the covers, so it throws the DivideByZeroException. Not one I
thought about since, VB.CLASSIC didn't have a real decimal type (it
was a variant sub type)....

Thanks for that, Kerry.
 

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