2

It's my understanding that IllegalArgumentException doesn't need to be thrown because it's a runtime error, and therefore in theory there shouldn't be a way to recover from it. My question is should it be thrown in the throw close? I've tried and it complies both ways.

With throws

public static void test() throws IllegalArgumentException
{
    throw new IllegalArgumentException();
}

Without throws

public static void test()
{
    throw new IllegalArgumentException();
}
1
  • It's not required, so no.
    – Kayaman
    May 27, 2014 at 17:19

6 Answers 6

2

As others have commented, it is strictly not necessary since IllegalArgumentException extends RuntimeException, i.e. an unchecked exception. However, it is considered a good habit to document this behavior by adding @throws to the Javadoc, especially if you are developing a library that will be used by others. For example, take a look at the declaration of the get(int index) method of the java.util.List interface:

/**
 * Returns the element at the specified position in this list.
 *
 * @param index index of the element to return
 * @return the element at the specified position in this list
 * @throws IndexOutOfBoundsException if the index is out of range
 *         (index < 0 || index >= size())
 */
E get(int index);

Note that the method signature does not have a throws clause.

1

Runtime Exceptions could be thrown but there is no real need for it. In the other hand Checked Exceptions if thrown in the body of a method should be declared in the signature of that method. In that case, the method without declaring the "throws" will not compile.

For the Runtime Exception try to call the test method, you'll see that the exception is thrown in all cases.

public static void main(String[] args){
YourClass.test();
}
1

If your function has a "contract" and checks incoming arguments, it can be a useful reminder. e.g.

public double squareRoot(double x) throws IllegalArgumentException {
  if (x < 0.0) 
     throw new IllegalArgumentException();

  happy path code here...
}

This behavior should also be javadoced (is that a word?) with a @throws clause. (As noted by @matsev)

0

Doesn't need to be thrown? Or indicated as thrown in the signature?

In the case of runtime exceptions it's a matter of documentation style. It doesn't need to be declared, but if it isn't, the possibility of an RTE should be documented manually instead of relying on automatic Javadoc generation.

0

As you noted yourself, there's no real need for it.

Although I don't usually do so, I've seen some projects that had a convention to declare the runtime exceptions methods could throw just to draw attention to that possibility and prompt programmers using these methods to handle said exceptions. Personally, I find it's more effective to note and explain these exceptions in a method's javadoc, but it all boils down to a question of taste.

0

I find it useful to declare even unchecked exceptions. Is it necessary? No, but neither is indenting your code. But it's nice to know what kind of exceptions can be thrown (Particularly if you're using a third party library which doesn't make the source code available) so you can do elegant error handling for different types of exception, in stead of having a blanket catch Throwableon there that just prints "Whoops something went wrong." to the log. This is seldom very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.