Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exception to throw?

Tags:

java

exception

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.

like image 826
nunos Avatar asked Sep 27 '10 22:09

nunos


People also ask

What type of exceptions can you throw?

We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.

When can exceptions be thrown?

Exceptions should be used for exceptional situations outside of the normal logic of a program. In the example program an out of range value is likely to be fairly common and should be dealt with using normal if-else type logic.

What causes an exception to be thrown?

An exception is thrown for one of three reasons: An abnormal execution condition was synchronously detected by the Java virtual machine. Such conditions arise because: evaluation of an expression violates the normal semantics of the language, such as an integer divide by zero, as summarized in §15.6.


2 Answers

You can throw a new IllegalArgumentException().

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

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

like image 161
Colin Hebert Avatar answered Oct 19 '22 21:10

Colin Hebert


The question to ask yourself first is whether you should be throwing at all and then, if so, whether it should be a checked or unchecked exception.

Unfortunately, there's no industry best practice on deciding these things, as shown by this StackOverflow answer:

In Java, when should I create a checked exception, and when should it be a runtime exception?

Nevertheless, there are some key considerations:

  • Your design/vision for how this method is supposed to work (Is it reasonable/normal for the method to be called with 0-size list)?

  • Consistency with other methods in the class/package

  • Compliance with your applicable coding standard (if any)

My opinion:

  • Return Double.NAN or 0 If calling the method with a 0-size list is reasonable/expected/normal, I'd consider returning Double.NAN or 0 if 0 is appropriate for your problem domain.

  • Throw anIllegalArgumentException If my design says that checking for an empty List is strongly the responsibility of the caller and the documentation for the method is going to clearly state that it is the responsibility of the caller, then I'd use the standard unchecked IllegalArgumentException.

  • Throw a custom checked exception If the method is part of a statistics package or library where several statistics functions need to deal with an possible empty data set, I'd think this is an exception condition that is part of the problem domain. I'd create a custom (probably checked) exception (e.g. EmptyDataSetException) to be part of the class/package/library and use it across all applicable methods. Making it a checked exceptions helps remind the client to consider how to handle the condition.

like image 12
Bert F Avatar answered Oct 19 '22 20:10

Bert F