I have a function which calculates the mean of a list passed as an argument. I would like to know which of Java exception should I throw when I try to compute the mean of a list of size 0.
public double mean (MyLinkedList<? extends Number> list)
{
if (list.isEmpty())
throw new ????????; //If I am not mistaken Java has some defined exception for this case
//code goes here
}
Thanks.
You can throw a
new IllegalArgumentException().Just don’t forget to pass a clear message as a first argument. This will really help you to understand what happend.
For example “Can’t use mean on an empty List”.