Home >Java >javaTutorial >In what scenarios does IllegalArgumentException occur in Java?

In what scenarios does IllegalArgumentException occur in Java?

WBOY
WBOYOriginal
2023-06-25 10:21:171087browse

As an important programming language, Java involves a large number of exception handling during the implementation process, among which IllegalArgumentException is a frequently used exception type. So, when does the IllegalArgumentException appear?

IllegalArgumentException means that the program will throw this exception when an illegal or incorrect parameter is passed to the method. Specifically, IllegalArgumentException usually occurs in the following scenarios:

1. Parameter type mismatch

When we pass a parameter to a method, if its type matches the formal parameter defined by the method If the types do not match, an IllegalArgumentException will be thrown. For example, if a parameter of type string is passed to a method that requires an argument of type integer, this exception will be thrown.

Sample code:

public class Test {
    public static void printNumber(int number) {
        System.out.println(number);
    }
    
    public static void main(String[] args) {
        String str = "123";
        printNumber(str); // 抛出IllegalArgumentException异常
    }
}

2. The parameter value is illegal

Sometimes, the value range of the parameter is given in the method, if the incoming parameter is not in this Within the scope, an IllegalArgumentException exception will also be thrown. For example, if an integer parameter less than 0 is passed to a method that only accepts parameters greater than or equal to 0, this exception will be thrown.

Sample code:

public class Test {
    public static void divide(int dividend) {
        if (dividend < 0) {
            throw new IllegalArgumentException("参数不合法,除数不能为负数");
        }
        System.out.println(10 / dividend);
    }
    
    public static void main(String[] args) {
        divide(-2); // 抛出IllegalArgumentException异常
    }
}

3. Null pointer exception

If a method does not allow the parameter to be null, but you pass a null value, an IllegalArgumentException will also be thrown. For example, if an empty string is passed to a method that does not allow an empty string, this exception will be thrown.

Sample code:

public class Test {
    public static void printString(String str) {
        if (str == null || str.isEmpty()) {
            throw new IllegalArgumentException("参数不合法,必须为非空字符串");
        }
        System.out.println(str);
    }
    
    public static void main(String[] args) {
        String str = null;
        printString(str); // 抛出IllegalArgumentException异常
    }
}

In the above scenario, if an IllegalArgumentException occurs, we can consider using the try-catch statement to catch and handle the exception. At the same time, when designing programs, we should also try to standardize the transfer of parameters to avoid exceptions caused by parameters.

The above is the detailed content of In what scenarios does IllegalArgumentException occur in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn