I have the following code:
public Trail getNewestTrail() {
return trails.stream().max(Comparator.comparing(Trail::getTimestamp)).orElseThrow(NoSuchElementException::new);
}
I am not seeing any error without having getNewestTrail declared as throwing the exception -- why?
NoSuchElementException
extends from java.lang.RuntimeException
, it is uncheched exception:
Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses)
you only need specify checked exception in method signature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With