Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the OptionalDouble.orElseThrow method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

In Java, the OptionalDouble object is a container object that may or may not contain a double value.

The OptionalDouble class is present in the java.util package.

The orElseThrow method will return the double value present in the OptionalDouble object. If the value is not present, then the supplier function passed as an argument is executed and an exception returned on the function is thrown.

Syntax

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

Argument

The argument is the function to be executed if the OptionalDouble object is emptyno value is present in the OptionalDouble object. The supplier function should return the exception to be thrown.

Return value

If the OptionalDouble object contains a value, then the value is returned. Otherwise, the exception returned by the supplier function is returned.

This method throws NullPointerException if no value is present and the supplier function is null.

Code

The code below denotes how to use the orElseThrow method.

import java.util.OptionalDouble;
class OptionalDoubleOrElseThrowExample {
public static void main(String[] args) throws Exception{
OptionalDouble optional1 = OptionalDouble.of(1.5);
System.out.println("Optional1 : " + optional1);
double val = optional1.orElseThrow(()-> {
return new Exception("no value present in Optional object");
});
System.out.println("Value at Optional1 is : " + val);
OptionalDouble optional2 = OptionalDouble.empty();
System.out.println("\nOptional2 : " + optional2);
val = optional2.orElseThrow(()-> {
return new Exception("no value present in Optional object");
});
}
}

Explanation

In the code above:

  • In line 1, we imported the OptionalDouble class.

  • In line 5, we created an OptionalDouble object with value 1.5 using the of method.

  • In line 7, we called the orElseThrow method on the optional1 object. For the orElseThrow method a supplier function that returns an Exception object is passed as an argument. This method returns 1 because the optional1 object contains the value.

  • In line 12, we used the empty method to get an empty OptionalDouble object. The returned object doesn’t have any value.

  • In line 12, we called the orElseThrow method on the optional2 object. For the orElseThrow method, a supplier function that returns an Exception object is passed as an argument. This method throws the exception returned by the supplier function because the optional2 object doesn’t have a value.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring