Optional orElseThrow() method in Java with examples Last Updated : 30 Jul, 2019 Comments Improve Suggest changes Like Article Like Report The orElseThrow() method of java.util.Optional class in Java is used to get the value of this Optional instance if present. If there is no value present in this Optional instance, then this method throws the exception generated from the specified supplier. Syntax: public <X> T orElseThrow(Supplier<X> exceptionSupplier) throws X extends Throwable Parameters: This method accepts supplier as a parameter of type X to throws an exception if there is no value present in this Optional instance. Return supplier: This method returns the value of this Optional instance, if present. If there is no value present in this Optional instance, then this method throws the exception generated from the specified supplier. Exception: This method throws NullPointerException if there is no value present in this Optional instance. Below programs illustrate orElseThrow() method: Program 1: Java // Java program to demonstrate // Optional.orElseThrow() method import java.util.*; import java.util.function.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.of(9455); // print supplier System.out.println("Optional: " + op); // orElseThrow supplier System.out.println( "Value by orElseThrow(" + "ArithmeticException::new) method: " + op.orElseThrow( ArithmeticException::new)); } } Output: Optional: Optional[9455] Value by orElseThrow(ArithmeticException::new) method: 9455 Program 2: Java // Java program to demonstrate // Optional.orElseThrow() method import java.util.*; import java.util.function.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.empty(); // print supplier System.out.println("Optional: " + op); try { // orElseThrow supplier System.out.println( "Value by orElseThrow(" + "ArithmeticException::new) method: " + op.orElseThrow( ArithmeticException::new)); } catch (Exception e) { System.out.println(e); } } } Output: Optional: Optional.empty java.lang.ArithmeticException Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#orElseThrow-java.util.function.Supplier- Comment More infoAdvertise with us Next Article Optional orElseThrow() method in Java with examples S ShubhamMaurya3 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Optional Practice Tags : Java Similar Reads Optional orElse() method in Java with examples The orElse() method of java.util.Optional class in Java is used to get the value of this Optional instance, if present. If there is no value present in this Optional instance, then this method returns the specified value. Syntax: public T orElse(T value) Parameters: This method accepts value as a pa 2 min read OptionalInt orElseGet() method in Java with examples The orElseGet(java.util.function.IntSupplier) method helps us to get the value in this OptionalInt object. If a value is not present in this OptionalInt, then this method returns the result produced by the supplying function, passed as the parameter Syntax: public int orElseGet(IntSupplier supplier) 2 min read OptionalLong orElseGet() method in Java with examples The orElseGet(java.util.function.LongSupplier) method helps us to get the value in this OptionalLong object. If a value is not present in this OptionalLong, then this method returns the result produced by the supplying function, passed as the parameter Syntax: public long orElseGet(LongSupplier supp 2 min read Optional or() method in Java with examples The or() method of java.util.Optional class in Java is used to get this Optional instance if any value is present. If there is no value present in this Optional instance, then this method returns an Optional instance with the value generated from the specified supplier. Syntax: public Optional<T 2 min read OptionalInt orElseThrow(Supplier) method in Java with examples The orElseThrow(Supplier) method of OptionalInt class used to get the value contained by OptionalInt. 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 paramete 2 min read Like