OptionalDouble orElseThrow(Supplier) method in Java with examples
The orElseThrow(Supplier) method of OptionalDouble class used to get the value contained by OptionalDouble. If a value is present, this method returns the value, otherwise, this method throws an exception produced by the exception supplying function. The exception Supplier function is passed as a parameter.
Syntax:
public <X extends Throwable> double
orElseThrow(Supplier<X> exceptionSupplier)
throws X extends Throwable
Parameters: This method accepts one parameter exceptionSupplier which is the supplying function that produces an exception to be thrown.
Return value: This method returns the value, if present.
Exception: This method throw following Exceptions:
- X – if no value is present.
- NullPointerException – if no value is present and the exception supplying function is null
- X extends Throwable
Below programs illustrate orElseThrow(Supplier) method:
Program 1:
// Java program to demonstrate // OptionalDouble.orElseThrow(Supplier) method import java.util.OptionalDouble; public class GFG { public static void main(String[] args) { // create a OptionalDouble OptionalDouble opDouble = OptionalDouble.of(0.268924); // apply orElseThrow(Supplier) double value = opDouble.orElseThrow(ArithmeticException::new); System.out.println("value " + value); } } |
Output:

Program 2:
// Java program to demonstrate // OptionalDouble.orElseThrow(Supplier) method import java.io.IOException; import java.util.OptionalDouble; public class GFG { public static void main(String[] args) { // create a OptionalDouble OptionalDouble opDouble = OptionalDouble.empty(); // apply orElseThrow(Supplier) double value; try { value = opDouble.orElseThrow(IOException::new); } catch (IOException e) { System.out.println("Exception " + e); } } } |
Output:

Recommended Posts:
- OptionalDouble ifPresentOrElse() method in Java with examples
- OptionalDouble getAsDouble() method in Java with examples
- OptionalDouble isPresent() method in Java with examples
- OptionalDouble equals() method in Java with examples
- OptionalDouble hashCode() method in Java with examples
- OptionalDouble empty() method in Java with examples
- OptionalDouble of(double) method in Java with examples
- OptionalDouble orElseThrow() method in Java with examples
- OptionalDouble orElseGet() method in Java with examples
- OptionalDouble toString() method in Java with examples
- OptionalDouble stream() method in Java with examples
- OptionalDouble ifPresent(DoubleConsumer) method in Java with examples
- OptionalDouble orElse(double) method in Java with examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



