Home

IllegalArgumentException

IllegalArgumentException is a runtime exception in the Java programming language indicating that a method has been passed an illegal or inappropriate argument. It is a subclass of RuntimeException, and therefore is unchecked. It signals that a precondition of a method has been violated by the caller, such as an argument value being out of range, of an invalid type, or otherwise inconsistent with the method’s contract. As a result, callers are not required to catch it, although they may choose to handle it or let it propagate to reveal programming errors.

Common usage patterns include validating parameters at the start of a method and throwing new IllegalArgumentException

The class provides several constructors: IllegalArgumentException(), IllegalArgumentException(String message), IllegalArgumentException(String message, Throwable cause), and IllegalArgumentException(Throwable cause).

There is no direct alternative language-wide equivalent; other platforms have similar concepts, such as ArgumentException in

with
a
descriptive
message
when
constraints
are
violated.
For
null
values
the
conventional
choice
is
often
NullPointerException
or
to
use
Objects.requireNonNull,
depending
on
the
contract.
NumberFormatException
is
a
subclass
of
IllegalArgumentException,
used
when
a
string
cannot
be
parsed
as
a
numeric
value.
In
API
design,
documenting
argument
constraints
in
the
method’s
Javadoc
and
preferring
early
validation
helps
maintain
clear
error
reporting.
.NET
or
ValueError
in
Python,
but
IllegalArgumentException
is
specific
to
Java’s
standard
exception
hierarchy.