I am worried that this is an exception at runtime, so it should probably be used sparingly. Standard use case:
void setPercentage(int pct) { if( pct < 0 || pct > 100) { throw new IllegalArgumentException("bad percent"); } }
But it looks like this would force the following design:
public void computeScore() throws MyPackageException { try { setPercentage(userInputPercent); } catch(IllegalArgumentException exc){ throw new MyPackageException(exc); } }
To return to checking the exception.
Ok, but let it go. If you give bad input, you get a runtime error. So, firstly, it is actually a rather complicated policy to implement evenly, because you may need to do the exact opposite conversion:
public void scanEmail(String emailStr, InputStream mime) { try { EmailAddress parsedAddress = EmailUtil.parse(emailStr); } catch(ParseException exc){ throw new IllegalArgumentException("bad email", exc); } }
And worse - when checking 0 <= pct && pct <= 100 you can expect the client code to do statically, this is not the case for more advanced data, such as an email address or, even worse, something that needs to be checked in the database, so the client’s common code cannot be pre-checked.
So basically what I am saying, I do not see a meaningful consistent policy of using IllegalArgumentException . It does not seem to be used, and we must adhere to our own checked exceptions. What could be useful for this?
java exception illegalargumentexception
djechlin Mar 04 '13 at 18:34 2013-03-04 18:34
source share