Java / Java8 streams
How do I pass custom message for the exception when thrown using orElseThrow method?
We can pass the custom message to the Exception constructor when created as shown below.
orElseThrow(() -> new EntityNotFoundException("Entity doesn't Exist"));
Example:
import java.util.Optional; public class OrElseThrowCustomMessage { public static void main(String[] args) { Optional<String> str = Optional.empty(); str.orElseThrow(()-> new NullPointerException("Empty String")); System.out.println(str); } }
More Related questions...