It's not that you need to return an int when you want to throw an exception. It's that the compiler "sees" a path to the bottom of the method (your for-loop ends, but pass is not false) which has no valid return value and does not throw an exception. It basically sees:
for () {
if () return an int; // valid return
}
if () throw an exception; // exception thrown
// INVALID -- no return, no exception
}
Java doesn't intuit the fact that the "if" expression at the bottom of your method will always evaluate to "true" -- because "pass" can only be set to true in a block of code that returns from the method right away. There is no path out of the for-loop which has "pass" set to anything other than false. But the way the code is structured, the compiler sees a path in the code at the bottom of the method that is not compatible with the required return type. So it complains.
You can help it out by making it clear that if your for-loop ends without returning, then you know pass is false anyway. You don't need that if-statement:
} // for-loop ends
throw new NoSuchElementException();
} // method ends
Then you don't need an unreachable statement to return an int at the bottom. The compiler can see that there is no path to the bottom of the method without either a valid return value or throwing an exception.
Two other minor notes:
(1) You don't need to explicitly compare booleans to constant values, as they are typed properly to be used directly in a boolean expression. If you were keeping the "if (pass == false)" statement, it would be better written "if ( ! pass )".
(2) You don't need the value of "pass" at all. You can remove the variable entirely, since you don't do anything with the value once the if-statement is removed. Your entire method would be:
for (int i=0; i
throw new NoSuchElementException();