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.