[Q]function doesnot declare 'throws...' even though it can potentially throw an exception...

P

Paul

Hi,

Anybody can explain to me why nextToken has no 'throws NoSuchElementException'??

StringTokenizer.java
public String nextToken() {
/*
* If next position already computed in hasMoreElements() and
* delimiters have changed between the computation and this invocation,
* then use the computed value.
*/

currentPosition = (newPosition >= 0 && !delimsChanged) ?
newPosition : skipDelimiters(currentPosition);

/* Reset these anyway */
delimsChanged = false;
newPosition = -1;

if (currentPosition >= maxPosition)
throw new NoSuchElementException();
int start = currentPosition;
currentPosition = scanToken(currentPosition);
return str.substring(start, currentPosition);
}
 
A

Adam Maass

Paul said:
Hi,

Anybody can explain to me why nextToken has no 'throws NoSuchElementException'??

StringTokenizer.java
public String nextToken() {
/*
* If next position already computed in hasMoreElements() and
* delimiters have changed between the computation and this invocation,
* then use the computed value.
*/

currentPosition = (newPosition >= 0 && !delimsChanged) ?
newPosition : skipDelimiters(currentPosition);

/* Reset these anyway */
delimsChanged = false;
newPosition = -1;

if (currentPosition >= maxPosition)
throw new NoSuchElementException();
int start = currentPosition;
currentPosition = scanToken(currentPosition);
return str.substring(start, currentPosition);
}

NoSuchElementException is a subclass of RuntimeException. RuntimeException
and its subclasses do not need to be declared in the method signature. They
are "unchecked" exceptions.

-- Adam Maass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top