Exceptions

Published

2023-07-31

Exceptions

StatisticsError

The statistics module has its own type of exception, StatisticsError. You may encounter this if you try to find the mean, median, or mode of an empty list.

>>> statistics.mean([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../python3.10/statistics.py", line 328, in mean
    raise StatisticsError('mean requires at least one data 
    point')
statistics.StatisticsError: mean requires at least one data 
point

This is also raised if you specify less than one quantile.

>>> statistics.quantiles([3, 6, 9, 5, 1], n=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../python3.10/statistics.py", line 658, 
    in quantiles
    raise StatisticsError('n must be at least 1')
statistics.StatisticsError: n must be at least 1

StatisticsError is actually a more specific type of ValueError.

Exceptions when using Matplotlib

There are many different exceptions that could be raised if you make programming errors while using Matplotlib. The exception and how to fix it will depend on context. If you encounter an exception from Matplotlib, your best bet is to consult the Matplotlib documentation.

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).