# Codesmell for java Optional.orElseThrow() with null supplier

**URL:** <https://community.sonarsource.com/t/codesmell-for-java-optional-orelsethrow-with-null-supplier/60303>\
**Category:** New rules / language support\
**Tags:** java\
**Created:** [March 22, 2022, 8:49am UTC](https://community.sonarsource.com/t/codesmell-for-java-optional-orelsethrow-with-null-supplier/60303 "2022-03-22T08:49:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reitzmichnicht](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/reitzmichnicht/32/288_2.png) [@reitzmichnicht](https://community.sonarsource.com/u/reitzmichnicht)\
**Post date:** [March 22, 2022, 8:49am UTC](https://community.sonarsource.com/t/codesmell-for-java-optional-orelsethrow-with-null-supplier/60303/1 "2022-03-22T08:49:22Z")

</div>

Hi

Just have seen this in our code:

```auto
Optional<String> foo = Optional.ofNullable("bar");
var s = foo.orElseThrow();

```

There is no exception supplier defined inside the orElseThrow() invocation, according to documentation this will lead to a NullPointerException. The compiler does not complain about it.

I think this is a code smell that should be detected by sonar because this always indicates that the developer missed to declare the exception to be thrown.

Kind regards,  
Michael

---

<div class="post-metadata">

**Author:** ![SebastianHungerecker](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/sebastianhungerecker/32/11178_2.png) [@SebastianHungerecker](https://community.sonarsource.com/u/SebastianHungerecker)\
**Post date:** [March 28, 2022, 5:24pm UTC](https://community.sonarsource.com/t/codesmell-for-java-optional-orelsethrow-with-null-supplier/60303/4 "2022-03-28T17:24:57Z")

</div>

Hi Michael,

Thank you for your suggestion.

Just to be clear: Since the optional in your example has a value, no exception will be thrown and `s` will have the value “bar”. An exception would only be thrown if the optional were empty. And in that case it would be a `NoSuchElementException`, not a `NullPointerException`.

And as far as I can tell, there’s nothing wrong with that. It seems perfectly reasonable to me that someone might not want to define a custom exception (or message) to be thrown here and would be okay with a `NoSuchElementException`. I would not consider this a code smell.

If you feel strongly that you always want an exception to be specified with `orElseThrow` in your code base, you can define a custom rule for that, but I don’t think this makes sense as a general rule.

Cheers,  
Sebastian

---

<div class="post-metadata">

**Author:** ![reitzmichnicht](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/reitzmichnicht/32/288_2.png) [@reitzmichnicht](https://community.sonarsource.com/u/reitzmichnicht)\
**Post date:** [April 1, 2022, 8:13pm UTC](https://community.sonarsource.com/t/codesmell-for-java-optional-orelsethrow-with-null-supplier/60303/5 "2022-04-01T20:13:30Z")

</div>

You are right, I missed the overload of orElseThrow() because I looked to the outdated java8 doc.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [April 8, 2022, 8:14pm UTC](https://community.sonarsource.com/t/codesmell-for-java-optional-orelsethrow-with-null-supplier/60303/6 "2022-04-08T20:14:08Z")

</div>

Closed on April 8, 2022, 8:14pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
