Illegal Argument Exception

A Java exception which should be deliberately thrown by methods that don't like their parameters. It extends RuntimeException, which means it does not need to be caught.

The singular name notwithstanding, can represent unsatisfied constraint between more parameters. The more you use and check the parameters, the more you move towards exception in the method invocation proper.

In many cases, code that is throwing NullPointerException should be argument-checking and throwing this, with a decent explanitory message.

See: IllegalStateException


I always want to "throw" this exception (preferably at my opponents' heads) during philosophical debates. -- SunirShah


I actually do prefer throwing NullPointerException when one of the arguments of a method is null, but filling in the explanatory message. To me, its semantic is more correct: you tell the caller it passed a null parameter, which is, in many case, the sign that something is wrong in the caller code. -- Julien Delfosse


Null is not an "illegal argument" it is a non-argument. NPE is specific to the problem and IAE is not.

The number of times that null would be valid is relatively small. Seems like the exceptional cases (where null is valid) are the ones I should say something about.

If I attempt to do something with a null that you pass me I get NPE for free without writing boilerplate.

NPE is expected behavior when I attempt to do something with null.

The cost of the VM throwing an NPE when it attempts to dereference your null arg is not significant. The only reason checking the arg would be a performance advantage is if you did a bunch of expensive stuff before you attempted to use it. In that case your method is probably doing too much.

-- AdamSroka?


EditText of this page (last edited November 12, 2014) or FindPage with title or text search