How to catch StatisticsError?

Viewed 720

I have problems in catching StatisticsError. Is it a bug or am I missing something?

from statistics import mean

try:
    mean([])
except StatisticsError:
    print(0)

---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-2-4fe8be631b28> in <module>
      3 try:
----> 4     mean([])
      5 except StatisticsError:

~/anaconda/lib/python3.6/statistics.py in mean(data)
    310     if n < 1:
--> 311         raise StatisticsError('mean requires at least one data point')
    312     T, total, count = _sum(data)

StatisticsError: mean requires at least one data point

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
<ipython-input-2-4fe8be631b28> in <module>
      3 try:
      4     mean([])
----> 5 except StatisticsError:
      6     print(0)

NameError: name 'StatisticsError' is not defined
0 Answers
Related