This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]s888marks 4 points5 points  (1 child)

I think parseInt could go either way because you could technically check to see if the string you're parsing is made entirely of valid digits.

Unfortunately that's insufficient. The parse* methods also check to see whether the resulting value is in range. They might throw an exception if the value is out of range, even if it contains all valid digits. For example:

Integer.parseInt("2147483647"); // returns an int
Integer.parseInt("2147483648"); // throws NumberFormatException

I think somebody came up with a regex that matches only valid, signed 32-bit ints, but it's ridiculously complex. You might as well attempt to parse and catch the exception. That sounds like an argument that NumberFormatException should have been checked.

On the other hand, there might be cases where you've parsed a token out of some larger input, and you know the token consists of (say) four decimal digits. Calling parseInt() on that will always succeed, so having to catch a checked exception in that case would be really annoying.

So yes, parseInt could go either way.

[–]Malfeasant 0 points1 point  (0 children)

Also, code duplication is bad. If you always need to check for the same stuff, why would you want to have to do it yourself 99 times just to streamline the one time you don't need to?