如何避免Exception
在尝试从 anOptional
或在使用时显式地抛出一个值Optional.get
?
目前,这可能通过 API 得到保护orElseThrow
:
// exception could be replaced with any other
Integer opt = anyOddInStream.orElseThrow(
() -> new NoSuchElementException("No value present"));
然而,get
在尝试访问类似
// the following not only just fails but throws an exception as well
Integer previous = anyOddInStream.get();
如果一个人想确保其中Optional
一个有一个值,如果没有,他们不想继续null
向前传播怎么办?