Should I put throws IllegalArgumentException at the function?

17,279

Solution 1

RuntimeExceptions like IllegalArgumentException are used to indicate programming errors. The program itself should rarely be able to handle it. Someone needs to manually fix the code.

Potential RuntimeExceptions should be documented somehow in the function contract (i.e. javadoc), either with the explicit @throws, or while describing the inputs. If you don't have a javadoc for the function, you may want to add the throws clause just to document the potential pitfalls of using the function, but in general adding throws clauses for runtime exceptions is discouraged.

If giving a wrong length is not actually a programming error, but is an exception situation, I would create a new checked exception (such as BadLengthError). If it is not an exceptional situation, don't use exceptions for flow control.

Solution 2

There's two types of exceptions:

runtime exceptions (like IllegalArgumentException and NullPointerException for example) don't need to be explicitly caught, because they 'shouldn't happen'. When they do, of course, you need to handle them somewhere.

regular exceptions NEED to be caught or declared to be thrown because they represent a more intrinsically difficult kind of error.

Solution 3

You need to read up on Unchecked Exceptions - exceptions which inherit from RuntimeException. They don't need to be declared in the method header.

http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html

The last paragraph sums it up:

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Solution 4

IllegalArgumentException (along with some others, for example NullPointerException) are examples of a RuntimeException. This type of exception is not what's known as a checked exception. Java requires that methods declare which checked exceptions they throw and that if a called method might throw a checked exception, the calling method should either declare that it throws the exception itself, or catch and handle it.

As such, my concrete recommendation would be no, don't declare it. Certainly, though, you don't want to be catching it. In most cases, you also don't want to be throwing it unless this is unexpected behaviour. If it's quite normal and reasonable for the the method to be getting a value it doesn't like, an Exception is the wrong way to handle it.

You might also want to consider making use of assertions.

Solution 5

the first point when understanding exceptions is that they are for exceptional situations. While thinking about your method you must ask: "Should this method throws an exception if an exceptional value is passed through?" If the answer is "yes", put it in your method declaration. I don't know if you get the idea, but it's kind of simple. It's just a matter of practice.

Share:
17,279
Ismail Marmoush
Author by

Ismail Marmoush

You can check my website and blog https://marmoush.com

Updated on June 11, 2022

Comments

  • Ismail Marmoush
    Ismail Marmoush almost 2 years

    I'm building a scientific software with lots of calculations and of course arguments can have wrong lengths etc... So I used IllegalArgumentException class as it seemed right name for the issue, but should I put the throws IllegalArgumentException at the function definition ?

    I am asking this because after I wrote it, the Eclipse Editor didn't ask me to surround the function with try and catch. I thought this is how try and catch were enforced. I've read the Exception handling tutorial at Java.com yet I'm not sure I understood the part regarding my question right though.

  • axcdnt
    axcdnt about 13 years
    By the way, there's a good reference I'd used while learning: en.wikibooks.org/wiki/Java_Programming/… Ya Matt, you're right. I've just told this way for an initial understanding, but maybe it's no good because it can confuse with method return.
  • Ismail Marmoush
    Ismail Marmoush about 13 years
    "an Exception is the wrong way to handle it." what is the right way then, can you elaborate more please ?