A DESCRIPTION OF THE PROBLEM :
The documentation for the Scanner class claims that the Scanner.next(Pattern) method should only throw one of two exceptions:
NoSuchElement
and
IllegalState
However, a simple test will show that if the Scanner encounters something that does not match the pattern, it will throw an InputMismatchException.
Technically, InputMismatchException is a subtype of NoSuchElementException, so the documentation isn't wrong, just misleading. Other methods that throw InputMismatchExceptions explicitly call out this exception (e.g. nextInt).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.
...
Throws:
InputMismatchException - if the next token does not match
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
ACTUAL -
Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.
...
Throws:
NoSuchElementException - if no such tokens are available
IllegalStateException - if this scanner is closed
URL OF FAULTY DOCUMENTATION :
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next(java.lang.String)
The documentation for the Scanner class claims that the Scanner.next(Pattern) method should only throw one of two exceptions:
NoSuchElement
and
IllegalState
However, a simple test will show that if the Scanner encounters something that does not match the pattern, it will throw an InputMismatchException.
Technically, InputMismatchException is a subtype of NoSuchElementException, so the documentation isn't wrong, just misleading. Other methods that throw InputMismatchExceptions explicitly call out this exception (e.g. nextInt).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.
...
Throws:
InputMismatchException - if the next token does not match
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
ACTUAL -
Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.
...
Throws:
NoSuchElementException - if no such tokens are available
IllegalStateException - if this scanner is closed
URL OF FAULTY DOCUMENTATION :
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next(java.lang.String)