Why is 0/0
throwing Overflow error
in VBA, while in .Net languages it is simply a Division by 0
error?
E.g., in C#
it is a System.DivideByZeroException
static void Main()
{
int k = 0;
int p = 0;
Console.WriteLine(k/p);
}
Div/0
error exists in VBA
. But 0/0
gives an overflow exception, while anything else divided by 0 gives a Div/0
exception:
Public Sub TestMe()
'Integer
PrintAndCheck (11) '- Division by zero error
'Double
PrintAndCheck (0.9) '- Division by zero error
'Long
PrintAndCheck (50000) '- Division by zero error
'String
PrintAndCheck ("1.1") '- Division by zero error
'----------------------------------------------------
'----------------BUT---------------------------------
'----------------------------------------------------
'Integer
PrintAndCheck (0) '- Overflow?
End Sub
Public Sub PrintAndCheck(lngDivisor As Variant)
On Error Resume Next
Debug.Print lngDivisor / 0
Debug.Print Err.Description & " from type -> " & VarType(lngDivisor)
On Error GoTo 0
End Sub
That's what you get in the immediate window:
Division by zero from type -> 2
Division by zero from type -> 5
Division by zero from type -> 3
Division by zero from type -> 8
Overflow from type -> 2
Public Sub TestMe()
On Error Resume Next
Debug.Print Evaluate("0/0") 'Division by 0 error (CVErr(xlErrDiv0)=2007)
Debug.Print 0 \ 0 'Division by 0 error
Debug.Print Err.Description
On Error GoTo 0
End Sub
In VBA, Overflow (Error 6) is a run-time error that occurs when you specify a number to the variable that is out of the range of numbers which that data type can take. In simple words, this error occurs when you go out of the range for a variable's type.
The data type Byte can hold values from 0 to 255. So it causes an error. To fix the error, we either change the data type or reduce the value assigned to the variable “Number.”
To correct this error Make sure that results of assignments, calculations, and data type conversions are not too large to be represented within the range of variables allowed for that type of value, and assign the value to a variable of a type that can hold a larger range of values, if necessary.
By far the most common cause of an overflow error in VBA is using an Integer variable type for a row number exceeding 32,767. Integer variables have only 16 bits, and so are limited to the range ±32,767. To avoid such problems, use the Long variable type instead.
Will try to summarize the answers from the comments:
In VBA 0/0 throws an Overflow exception, because 0/0 is a specific case of division by 0. Thus, it is a good idea to throw a different exception than the standard Division by zero error
.
In VBA Evaluate("0/0")
returns a Division by zero error
, because Evaluate does not raise an error, it returns a Variant value with an error flag, and there is no "overflow" flag available.
In VBA integer division 0\0 returns a Division by zero error
, because the result should be an integer value and #IND is a floating point value. As far as #IND cannot be returned, it gives the next best thing - Division by zero error
.
More reading concerning 0/0 in other languages:
How to produce a NaN float in c?
VB.NET
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With