I have defined this stream:
public int sumNumbers(int[] numbers) {
return IntStream.of(numbers)
.filter(n -> n <= 1000)
.sum();
}
Where I'm summing all integers that are not higher than 1000. But now what I want to do is, throw an exception if any element of the array is negative.
I know how to do it in the old fashioned mode, but I was wondering if there's any mechanism with Stream
and .filter()
where I can define a filter and an exception case for that filter
Just to clarify I want to throw an exception, and not control a runtime exception as the other question does.
The idea here is that if my filter is true in:
filter(n -> n < 0 throw Exception)