Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Streams and Functional

Java Optional.orElse() and orElseThrow()

Reference for getting a value out of a Java Optional: get(), orElse(), orElseGet(), orElseThrow() and or(), with the eager-versus-lazy difference explained.

Once you have an Optional, you eventually need the value inside, or a sensible fallback. Java gives you several ways to unwrap it, and they differ in what happens when the Optional is empty.

orElse(other) returns a default, orElseGet(supplier) computes the default only when needed, and orElseThrow() throws an exception. get() also exists, but it is the one to avoid: its name hides the fact that it can throw. Since Java 10, orElseThrow() with no argument is the clear, preferred replacement for get().

Java
String name = optionalName.orElse("anonymous");
String id   = optionalId.orElseThrow(() -> new IllegalStateException("no id"));

Try it

Java Choosing a fallback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Throwing when a value is required

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public T get()
public T orElse(T other)
public T orElseGet(Supplier<? extends T> supplier)
public T orElseThrow()
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
Method If present If empty
get() the value throws NoSuchElementException
orElse(other) the value other (always evaluated before the call)
orElseGet(supplier) the value the supplier's result (supplier called only now)
orElseThrow() the value throws NoSuchElementException (Java 10)
orElseThrow(supplier) the value throws the exception the supplier creates
or(supplier) this Optional the Optional the supplier returns (Java 9)

Throws: NoSuchElementException for get() and orElseThrow(); any X for the supplier variant. When the Optional is empty, orElseThrow(supplier) throws NullPointerException if the supplier itself returns null.

Usage notes

  • orElse is eager. Its argument is evaluated before the method runs, whether or not the Optional is empty. If building the default has a cost or a side effect (database call, logging, object creation), use orElseGet.
  • Cheap constants are fine with orElse. orElse("") or orElse(0) creates nothing expensive, and it reads better than a lambda.
  • Prefer orElseThrow() over get(). They behave identically, but the name tells readers an exception is possible. Tools and linters often flag get() without a prior isPresent().
  • Unchecked or checked. The supplier version lets you throw a checked exception too; the method's throws X clause then forces callers to handle it.
  • isPresent() then get() is usually a sign you want orElse*, map or ifPresent instead.
  • Returning null from orElse(null) is allowed, but turns the Optional back into a nullable reference; keep it at boundaries with old APIs.
  • or() returns an Optional, so it chains fallbacks without unwrapping: first cache, then database, then default.