At first
The cause and solve for IllegalArgumentException is very simple.
Once you understand the basics, you can easily fix it.
What is IllegalArgumentException?
Exception thrown to indicate that invalid or inappropriate arguments were passed to a method.
For example, a value greater than or equal to 0 should be passed, but it will throw if a negative number is passed.
Cause
Let’s check the cause of occurrence with concrete code.
example case
As an example, set “-1” to the setLastModified method of the File class.
1 2 3 4 5 6 7 8 |
import java.io.File; public class Main { public static void main(String[] args) { File file = new File("dummy"); file.setLastModified(-1); // ← ★occurrence } } |
throw exception
Doing so will result in an IllegalArgumentException.
1 2 3 |
Exception in thread "main" java.lang.IllegalArgumentException: Negative time at java.base/java.io.File.setLastModified(File.java:1439) // ← ★cause at IllegalArgumentException.Main.main(Main.java:8) // ← ★occurrence |
why?
The reason is that the setLastModified method raised an IllegalArgumentException for numbers less than 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class File implements Serializable, Comparable<File> { ... public boolean setLastModified(long time) { if (time < 0) throw new IllegalArgumentException("Negative time"); // ← ★cause SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkWrite(path); } if (isInvalid()) { return false; } return fs.setLastModifiedTime(this, time); } |
Solve
It is necessary to pass the value of the condition under which IllegalArgumentExceptiong does not occur to the method.
In the above example, you pass a value greater than or equal to 0 to the setLastModifiedTime method.
If an IllegalArgumentExceptiong occurs, you can check the location where it occurred in the stack trace (output such as [throw exception]), and the location where it occurred is also written, so it is easy to follow.
Since the occurrence conditions are also written, it is only necessary to prevent inappropriate values from entering.
How to use IllegalArgumentException in your own class
If you want to create your own class and raise an IllegalArgumentException in it, do the following.
example 4 patterns
There are 4 patterns that combine the presence or absence of a message when an exception occurs and whether or not to pass the exception that causes it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package IllegalArgumentException; public class MyClass { private Integer value; private String name; private String code; private Integer number; // No detailed message public void setValue(Integer value) { if (name == null) { throw new IllegalArgumentException(); } this.name = name; } /** Exception in thread "main" java.lang.IllegalArgumentException at IllegalArgumentException.MyClass.setValue(MyClass.java:12) at IllegalArgumentException.MyClassUse.main(MyClassUse.java:6) */ // With detailed message public void setName(String name) { if (name == null) { throw new IllegalArgumentException("Can't set null."); } this.name = name; } /** [エラーメッセージを挿入した関係で、以降は行数の表記がずれています] Exception in thread "main" java.lang.IllegalArgumentException: Can't set null. at IllegalArgumentException.MyClass.setName(MyClass.java:20) at IllegalArgumentException.MyClassUse.main(MyClassUse.java:7) */ // Set a new exception to cause, without detail message public void setCode(String code) { if (code == null) { throw new IllegalArgumentException(new RuntimeException("code is null.")); } this.code = code; } /** Exception in thread "main" java.lang.IllegalArgumentException: java.lang.RuntimeException: code is null. at IllegalArgumentException.MyClass.setCode(MyClass.java:28) at IllegalArgumentException.MyClassUse.main(MyClassUse.java:8) Caused by: java.lang.RuntimeException: code is null. ... 2 more */ // Set a new exception with a detailed message and cause public void setNumber(Integer number) { if (number == null) { throw new IllegalArgumentException("Can't set null.", new RuntimeException("number is null.")); } this.number = number; } /** Exception in thread "main" java.lang.IllegalArgumentException: Can't set null. at IllegalArgumentException.MyClass.setNumber(MyClass.java:36) at IllegalArgumentException.MyClassUse.main(MyClassUse.java:9) Caused by: java.lang.RuntimeException: number is null. ... 2 more */ } |
Basically, I personally think that there are many patterns that just add a message to the IllegalArgumentException.
If you need details of the cause of the occurrence, you can set a new exception as shown in the example above, so please use it as necessary.
Conclusion
- An IllegalArgumentException is an exception thrown to indicate that you passed an invalid or inappropriate argument to a method.
- The cause is due to an invalid value being entered in the method.
- The solution is to follow from where it happened, check for appropriate values, and control the values that are passed.
- It is possible to throw an exception in your own class.
Also, when throwing, you can select whether to set a new exception that causes a message or not
Refer to
This is a book that can deepen your understanding of exceptions.
Exceptions in Java: Basics, advanced concepts, and real API examples (English Edition)
Comments