Programming Tips - Java: Should I use IllegalArgumentException when my function gets wrong values for parameters?

Date: 2019dec21 Language: Java Q. Java: Should I use IllegalArgumentException when my function gets wrong values for parameters? A. That sounds like what you are supposed to do but its unchecked So you can write:
void myFunc(int n) { // No `throws` is allowed if (n < 0) { throw new IllegalArgumentException("n can not be negative"); } // ... }
What's wrong with that? Notice the function has no `throws IllegalArgumentException` and that's allowed. So using the function:
myFunc(-1);
without a `catch` is also allowed. That means this program will crash when the exception is thrown. Maybe that's what you want but not usually. So I use checked exceptions for this kind of thing like:
void myFunc(int n) throws IOException { if (n < 0) { throw new IOException("n can not be negative"); } // ... }