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 parameter.
Syntax:
public <X extends Throwable> int
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:
Output:
👁 Image
Program 2: