Javatpoint Logo
Javatpoint Logo

IllegalArgumentException in Java example

Java, a widely used programming language, is known for its robustness and protection capabilities. However, even in the maximum cautiously crafted programs, mistakes can occur. One common type of error is the "Illegal Argument Exception." In this section, we are going to explore what this exception is, why it occurs, and offer examples with full programs to illustrate its behaviour.

What is Illegal Argument Exception?

In Java, an IllegalArgumentException is a runtime exception that takes place whilst a method is known as with an argument this is illegal or inappropriate. It means that the argument does no longer meet the approach's expectancies or violates its preconditions.


Exception in thread "main" java.lang.IllegalArgumentException: Name cannot be null
	at InvalidArgumentExceptionExample.addName(
	at InvalidArgumentExceptionExample.main(

We create an easy program that continues a list of names. The addName() method is designed to feature a name to the listing, but it assessments if the furnished name is null. If the name is null, an IllegalArgumentException is thrown with a descriptive error message.

In this situation, the program attempts to feature a null call to the list, triggering the IllegalArgumentException due to the fact the technique explicitly forbids null names.

Let's explore common eventualities where IllegalArgumentException may occur:

1. Null Arguments:


Exception in thread "main" java.lang.IllegalArgumentException: Text cannot be null
	at NullArgumentExceptionExample.printLength(
	at NullArgumentExceptionExample.main(

The printLength() method expects a non-null string, and if null is provided, an IllegalArgumentException is thrown.

Invalid Numeric Values:


Exception in thread "main" java.lang.IllegalArgumentException: Number must be non-negative
	at InvalidNumberArgumentExceptionExample.calculateSquareRoot(
	at InvalidNumberArgumentExceptionExample.main(

The calculateSquareRoot technique calculates the square root of quite a number however only accepts non-negative values. If a poor quantity is supplied, an IllegalArgumentException is thrown.

2. Empty or Invalid Collections:


Exception in thread "main" java.lang.IllegalArgumentException: List must be non-null and non-empty
	at InvalidCollectionArgumentExceptionExample.processList(
	at InvalidCollectionArgumentExceptionExample.main(

The processList() technique expects a non-null and non-empty listing. If a null or empty list is supplied, an IllegalArgumentException is thrown.

Best Practices to Handle IllegalArgumentException

To manage IllegalArgumentException gracefully, bear in mind the following first-rate practices:

  1. Validate Inputs Early: Ensure that enter validation is performed at the start of a way to seize invalid arguments early.
  2. Use Descriptive Error Messages: Provide clean and meaningful errors messages to assist developers in identifying the reason of the exception.
  3. Document Preconditions: Clearly file the preconditions for every approach, specifying the predicted ranges and kinds of arguments.
  4. Throwing Specific Exceptions: Use greater precise exceptions while available. If a technique expects a selected type, recollect throwing NullPointerException for null arguments and other appropriate exceptions for distinct invalid states.
  5. Custom Exception Handling: Implement custom exception handling to encapsulate the info of the exception and handle it in a way that makes feel on your application.

In Summary, understanding and handling IllegalArgumentException is critical for writing sturdy and reliable Java programs. By validating inputs, offering descriptive blunders messages, and following pleasant practices, builders can create code that isn't most effective purposeful but also resilient to unexpected inputs. As illustrated within the examples, catching and addressing unlawful arguments early in the development system contributes to the general balance and maintainability of Java applications.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA