Optionalnumber = Optional.of(10); int value = number.orElseThrow(() -> new IllegalArgumentException("Value not found"));
OptionalIn this example, the getResult method returns an Optional object. We then use the ifPresentOrElse method to execute a lambda expression that prints the value of the Optional object or throw an exception of type IllegalStateException if the Optional object is empty. The java.util.Optional class is part of the java.util package library in Java.result = getResult(); result.ifPresentOrElse( value -> System.out.println("Result: " + value), () -> throw new IllegalStateException("No result found") );