IllegalArgumentException in Java exampleJava, 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. InvalidArgumentExceptionExample.java Output: Exception in thread "main" java.lang.IllegalArgumentException: Name cannot be null at InvalidArgumentExceptionExample.addName(InvalidArgumentExceptionExample.java:12) at InvalidArgumentExceptionExample.main(InvalidArgumentExceptionExample.java:8) 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:NullArgumentExceptionExample.java Output: Exception in thread "main" java.lang.IllegalArgumentException: Text cannot be null at NullArgumentExceptionExample.printLength(NullArgumentExceptionExample.java:10) at NullArgumentExceptionExample.main(NullArgumentExceptionExample.java:6) The printLength() method expects a non-null string, and if null is provided, an IllegalArgumentException is thrown. Invalid Numeric Values:InvalidNumberArgumentExceptionExample.java Output: Exception in thread "main" java.lang.IllegalArgumentException: Number must be non-negative at InvalidNumberArgumentExceptionExample.calculateSquareRoot(InvalidNumberArgumentExceptionExample.java:10) at InvalidNumberArgumentExceptionExample.main(InvalidNumberArgumentExceptionExample.java:6) 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:InvalidCollectionArgumentExceptionExample.java Output: Exception in thread "main" java.lang.IllegalArgumentException: List must be non-null and non-empty at InvalidCollectionArgumentExceptionExample.processList(InvalidCollectionArgumentExceptionExample.java:11) at InvalidCollectionArgumentExceptionExample.main(InvalidCollectionArgumentExceptionExample.java:6) 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 IllegalArgumentExceptionTo manage IllegalArgumentException gracefully, bear in mind the following first-rate practices:
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. Next TopicIs the main() method compulsory in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India