OptionalLong orElseThrow() method in Java with examples
OptionalLong help us to create an object which may or may not contain a Long value.
The orElseThrow() method help us to get the value and if value is not present then this method will throw NoSuchElemenrException.
public Long orElseThrow()
Parameters: This method accepts nothing.
Return value: This method returns the Long value described by this OptionalLong.
Exception: This method throws NoSuchElementException if no value is present
Below programs illustrate orElseThrow() method:
Value extracted using orElseThrow() = 45213246
Exception thrown : java.util.NoSuchElementException: No value present
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.