Sunday, April 12, 2020

Java 8 Optional orElseThrow() Example | Throw Exception in Optional in Java 8

1. introduction


In this tutorial, We'll learn how to throw an exception if the option is empty. Optional API orElseThrow() method returns value from Optional if present. Otherwise, it will throw the exception created by the Supplier.

2. Syntax


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


Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new, ArithmeticException::new



3. Example to Throw Exception - orElseThrow() Program


Example to get the value from Optional if it has. It optional value is null then it throws NoSuchElementException.

package com.java.w3schools.blog.java8.optional;

import java.util.Optional;

public class OptionalorElseThrowExample {

 public static void main(String[] args) {

  Optional<String> o = Optional.ofNullable("Hello");
  String value = o.orElseThrow();
  System.out.println("Getting value from optional : " + value);

  Optional<String> o2 = Optional.ofNullable(null);
  String value2 = o2.orElseThrow();
  System.out.println("If optional value is null : " + value2);

 }

}

Output:

Getting value from optional : Hello
Exception in thread "main" java.util.NoSuchElementException: No value present
 at java.base/java.util.Optional.orElseThrow(Optional.java:382)
 at com.java.w3schools.blog.java8.optional.OptionalorElseThrowExample.main(OptionalorElseThrowExample.java:14)



4. Throwing Custom Exception and Runtime Exception


Throwing AgeLimitException exception

class AgeLimitException extends RuntimeException {

 public AgeLimitException(String meg) {
  super(meg);

 }

}

Custom exception throwing exception

Optional<String> o3 = Optional.empty();
String value3 = o3.orElseThrow(() -> new AgeLimitException("Optional is empty. No age value is specified"));
System.out.println("Optiona with custom exception " + value3);

Output:

Exception in thread "main" com.java.w3schools.blog.java8.optional.AgeLimitException: Optional is empty. No age value is specified
 at com.java.w3schools.blog.java8.optional.OptionalorElseThrowExample.lambda$0(OptionalorElseThrowExample.java:19)
 at java.base/java.util.Optional.orElseThrow(Optional.java:408)
 at com.java.w3schools.blog.java8.optional.OptionalorElseThrowExample.main(OptionalorElseThrowExample.java:19)


Throwing RuntimeException:

Optional<String> o4 = Optional.empty();
String value4 = o4.orElseThrow(ArithmeticException::new);
System.out.println("THrowing Arithmetic exception" + value4);

Output:

Exception in thread "main" java.lang.ArithmeticException
 at java.base/java.util.Optional.orElseThrow(Optional.java:408)
 at com.java.w3schools.blog.java8.optional.OptionalorElseThrowExample.main(OptionalorElseThrowExample.java:25)


5. Conclusion


In this article, We've seen how to throw an exception from the Optional API using orElseThrow() method.


GitHub Code

No comments:

Post a Comment