I have a scenario in which I want to abort the entire progress if any of the iterated objects have some certain bad condition (not valid or whatever). I don't want to just skip this specific object and go on with the rest.
As an example take reading a CSV file. If one of the lines have less/more elements than the header line, I will have problems mapping it. I then want to abort the process with an exception "csv file is invalid" which I catch in higher level to react properly.
I use something like this:
csvLines.forEach(line -> {
if (line.isValid()) {
doSomethingWith(line);
} else {
// ABORT MISSION, I REPEAT, ABORT!
// throw new CSVNotValidException("did not work :(");
}
});
I saw many SO posts regarding "jumping out of J8 foreach", but in there I only see how to extend the standard behavior etc. to somehow make it possible.
I want to know, why it is designed this way, that no checked Exception can be thrown (and catched in higher level afterwards) within a lambda expression in Java 8
I know technically, why this is the case (the functional interfaces simply don't define the throws part), but I don't understand, why it is chosen so. I only could explain this to myself, if you can solve all issues without the need of throwing an exception - but I don't actually think that this is the case.
P.S.: I don't want to use a RuntimeException (which would be possible to throw) as I don't like catching RuntimeExceptions. I want to use a checked Exception for this.