# java:S5778 detects false runtime exception

**URL:** <https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [March 19, 2021, 9:19am UTC](https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363 "2021-03-19T09:19:31Z")\
**Posts on this page:** 6\
**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 19, 2021, 9:19am UTC](https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363/1 "2021-03-19T09:19:31Z")

</div>

We are using Sonarqube 8.7.

The following code detects runtime exceptions in Collections.emptyList()

 ![2021-03-19 10_15_26-Window](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/0/5/05ac03abff6bdeac59aae013381f15b08e78ebea.png)

The match in the constructor itself is correct as it throws a runtime exception, but the 2 matches with Collections.emptyList() are wrong, because also the cast inside can not fail in my oppinion.

Kind regards,  
Michael

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [March 23, 2021, 10:38am UTC](https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363/2 "2021-03-23T10:38:40Z")

</div>

Hey @reitzmichnicht

Your report is a bit light in information. Can I suggest you look at the following post, and add more details?

> [@How to Report a False-positive / False-negative](https://community.sonarsource.com/t/how-to-report-a-false-positive/37022):
>
> Hey SonarSource Community! [False-positives happen](https://blog.sonarsource.com/false-positives-our-enemies-but-maybe-your-friends), as do false-negatives, and we’re eager to fix them. We are thrilled when our users report problems, so we can make our products better. What is a false-positive (FP)? A false-positive is when an issue is raised unexpectedly on code that should not trigger an issue, or where the suggested action doesn’t make any sense for the code. What is a false-negative (FN)? A false-negative is when an issue should be raised on a piece of code, but isn’t. …

---

<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 23, 2021, 12:14pm UTC](https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363/3 "2021-03-23T12:14:45Z")

</div>

I hope its obvious, here is some code example:

```
  // given
  public FooService(List<String> a, List<String> b) {
    Objects.requireNonNull(a, "a must not be null");
    Objects.requireNonNull(b, "b must not be null");
    if (a.isEmpty() && b.isEmpty()) {
      throw new IllegalArgumentException();
    }
  }

  // then this should not fail in java:S5778
  assertThatThrownBy(() -> new FooService(Collections.emptyList(), Collections.emptyList())).isInstanceOf(IllegalArgumentException.class);

```

So from my understanding Sonar thinks that Collections.emptyList() can throw an exception, I think it can not.

Kind regards,  
Michael

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [April 12, 2021, 10:00am UTC](https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363/5 "2021-04-12T10:00:31Z")

</div>

Hello @reitzmichnicht

The problem with this assertion is that it is not clear at first glance which method call/new class is supposed to throw the expected Runtime Exception. Even if I can guess that `Collections.emptyList()` is not throwing any Exception and that it should be the class creation, I had to manually check `emptyList()` code to be 100% sure about it.

That’s exactly the spirit of the rule: we want to avoid guesses and manual verifications in tests.

That being said, I understand your concern, there are indeed situations where it could seem superfluous. If you strongly believe the code is fine as it is, you can mark it as “Won’t fix”.

Hope it clarifies the situation.  
Quentin

---

<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 12, 2021, 12:53pm UTC](https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363/7 "2021-04-12T12:53:25Z")

</div>

That clarifies it.  
I had the assumption that the rule really analyzes if multiple exceptions can occur, but then it’s more related to readability.  
Maybe it makes sense to whitelist some methods that can be used safely.

Kind regardsm  
Michael

---

<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 19, 2021, 12:53pm UTC](https://community.sonarsource.com/t/java-s5778-detects-false-runtime-exception/40363/8 "2021-04-19T12:53:54Z")

</div>

Closed on April 19, 2021, 12:53pm UTC

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