This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author akima
Recipients akima
Date 2014-08-30.07:58:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1409385535.02.0.413020952628.issue22306@psf.upfronthosting.co.za>
In-reply-to
Content
1 / 0 (where both numbers are decimal.Decimal) produces a decimal.DivisionByZero exception as I would expect.  This is useful.  I can use a simple try except block to catch a potential division by zero error in my code.

0 / 0 (where both numbers are decimal.Decimal) produces a decimal.InvalidOperation exception.  This is undesirable.  I would expect another decimal.DivisionByZero exception.  This means that if I want to catch a division by zero error in my code using a try except block, I now have to catch exceptions for both decimal.DivisionByZero and decimal.InvalidOperation.  Presumably decimal.InvalidOperation can be raised in other scenarios, so catching it may result in masking a programming fault (which isn't just a division by zero: 0 / 0).

If you perform the same division but using standard Python integers instead of decimal.Decimal objects, the behaviour is exactly as you would expect: 0 / 0 and 1 / 0 both produce a ZeroDivisionError exception.

I have tested this in CPython 3.3.5, 3.2.3 and 2.7.3.  All versions produce the same behaviour.


Demonstration:


Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal as d
>>> d(1) / d(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/decimal.py", line 1323, in __truediv__
    return context._raise_error(DivisionByZero, 'x / 0', sign)
  File "/usr/lib/python2.7/decimal.py", line 3866, in _raise_error
    raise error(explanation)
decimal.DivisionByZero: x / 0
>>> d(0) / d(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/decimal.py", line 1322, in __truediv__
    return context._raise_error(DivisionUndefined, '0 / 0')
  File "/usr/lib/python2.7/decimal.py", line 3866, in _raise_error
    raise error(explanation)
decimal.InvalidOperation: 0 / 0
>>> 1 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> 0 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>


Here is the same demonstration but using a Python 3.2.3 interpreter:


Python 3.2.3 (default, Feb 27 2014, 21:31:18) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal as d
>>> d(1) / d(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/decimal.py", line 1300, in __truediv__
    return context._raise_error(DivisionByZero, 'x / 0', sign)
  File "/usr/lib/python3.2/decimal.py", line 3926, in _raise_error
    raise error(explanation)
decimal.DivisionByZero: x / 0
>>> d(0) / d(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/decimal.py", line 1299, in __truediv__
    return context._raise_error(DivisionUndefined, '0 / 0')
  File "/usr/lib/python3.2/decimal.py", line 3926, in _raise_error
    raise error(explanation)
decimal.InvalidOperation: 0 / 0
>>> 1 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 0 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
History
Date User Action Args
2014-08-30 07:58:55akimasetrecipients: + akima
2014-08-30 07:58:55akimasetmessageid: <1409385535.02.0.413020952628.issue22306@psf.upfronthosting.co.za>
2014-08-30 07:58:54akimalinkissue22306 messages
2014-08-30 07:58:54akimacreate