12

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

Edit: To make the whole story more interesting:

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
15
  • Have you explicitly tried 0 / 0? Commented Aug 3, 2017 at 13:23
  • @meowgoesthedog - in C# it does not compile, in VBA it gives Overflow Commented Aug 3, 2017 at 13:24
  • 12
    If every language behaved identically in all circumstances, there wouldn't be different languages. Since 0/0 is very poorly defined, there no single correct response to it. Commented Aug 3, 2017 at 13:32
  • 1
    It's an interesting question, which I think would be better if you left C# / .Net exceptions out of it, and re-worded it to compare VBA behavior when dividing by zero with a non-zero numerator VS. a zero numerator. Commented Aug 3, 2017 at 13:56
  • 4
    It is worth pointing out that 0 / 0 is a special case. The normal argument against calculations of the form x / 0 where x <> 0 is that there is no number y such that y * 0 = x, and that x / 0 is therefore undefined. This is not the case for 0, as 0 * 0 is indeed 0. So if VBA generates a different error for 0 / 0 as opposed to 1 / 0, I take my hat off to it! Commented Aug 3, 2017 at 14:18

2 Answers 2

2

Will try to summarize the answers from the comments:

  1. 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.

  2. 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.

  3. 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:

Sign up to request clarification or add additional context in comments.

Comments

1

https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/floating-point-division-operator

Besides the obvious differences in implementation of languages and the way VBA handles division, MS Doc link above expands on the reasons for overflow exception , that if the operand data types are integer then it will throw Overflow exception (Last statement below)

enter image description here

Alternatively there is \ division operator that checks the range for you and throws Division by zero exception

6 Comments

That's an interesting reading indeed, but it is for VB.Net. In VBA, a Single, Double or even a String value of 0/0 would throw an Overthrow error.
Wouldn't division using string become overloaded and converted to number?
@While it is true that this is for vb.net, the rationale for raising an overflow error as opposed to a division by zero error is doubtless the same. Still, it would be good to find a link to the VBA or at least VB6 documentation for this point.
@RYenugu - yes. The result would be a number. Or an error :)
@RYenugu - I understand, but in VBA zero is an Integer. See the updated version of the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.