0

I have some consistency issue with the Optionals in Java.

According to the documentation:

public T orElseThrow​(Supplier<? extends X> exceptionSupplier) throws X extends Throwable

If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.

But in my code it doesn't throw (and I've checked it for being null with the "isPresent()" method. What am I missing? Take a look at my code below and feel free to ask questions.

public AttractionOverview getAttractionOverviewById(int attraction_id) throws NoSuchElementException {
    Optional<AttractionOverview> overview = this.attractionOverviewRepository.findAttractionOverviewById(attraction_id);
    overview.isPresent();
    return overview.<RuntimeException>orElseThrow(RuntimeException::new);
}

(I have placed a breakpoint at the line: overview:isPresent() and according to the debugger the value (when I select an id that does not exist) the value is false: meaning according to how I interpret the documentation it should throw the provided exception.

EDIT Some more context: I'm working in a SpringBoot application with a JPARepository. The orElseThrows doesn't work there. But when I simply do this in Java:

public class OptionalThrows {
public static void main(String[] args) throws Exception {
    Optional<Object> empty = Optional.empty();
    Object aThrow = empty.orElseThrow(() -> new RuntimeException("Nothing to see here"));
    System.out.println(aThrow);
}

}

Then it works fine. Is it a springboot issue perhaps?

  • 1
    Can you show a [mcve]? – Sweeper Jun 24 '22 at 14:40
  • I just tried this in JShell and it threw the exception. – David Conrad Jun 24 '22 at 14:43
  • 2
    According to your code example the exception will be thrown if optional is empty. Does the caller of `getAttractionOverviewById` probably handle the exception, so the exception isn't rethrown? – ph1l Jun 24 '22 at 14:46
  • There are several things that are idiosyncratic about your code: You declare the method can throw NoSuchElementException but that is a RuntimeException, not a checked exception. You qualify a field with `this` although that may be a local code style requirement. You call `isPresent()` without assigning or testing the result, but I guess this is just for debugging. You have `` on the call to `orElseThrow` even though it is an `Optional`. But none of those things should cause the result you say you are getting. – David Conrad Jun 24 '22 at 14:47
  • I changed the Exception to a checked exception, but that just doesn't change anything either. The method getAttractionOverviewById doesn't handle anything. It just returns data from a database. Or null in case the ResultSet is empty. (I think: it's a JPARepository method). I just don't get it.. No matter what I do, the orElseThrow does not work. Sorry for the late response. EDIT: just tried with an empty optional and an null as param for Optional.ofNullable(o) and in both cases it also doesn't work. It's weird. – Jurgen Rutten Jun 24 '22 at 17:02
  • I tried your example and I get the `RuntimeException`. I suggest trying a very simple example (not using Spring) and see if it works. If it does, you can work back from there. Alternatively, please provide a full failing example repository here so someone can help you. – Tom Gregory Jul 12 '22 at 07:04
  • It's a spring problem. Has to do with reflection sadly. No direct solution. All my teammates are getting the same issue, so we decided to simply use a command-chain for our checks. – Jurgen Rutten Jul 12 '22 at 07:23

0 Answers0