I want to loop over a huge array and do a complicated set of instructions that takes a long time. However, if more than 30 seconds have passed, I want it to give up.
ex.
As what the comments said under the OP, takeWhile/dropWhile are missed in Java 8 (will be added in Java 9). There is no any reason to try to implement the logic by exception or other codes because the code just looks so ugly and total nonscenes even it's just for practice. I think using 3rd party library is a much, much better solution, for example StreamEx
StreamEx(source).takeWhile(() -> System.currentTimeMillis() <= start + 30000)
.forEach(e -> { ... });