Cover Image for SonarQube で squid:S1162 が発生した時の対処方法

SonarQube で squid:S1162 が発生した時の対処方法

この記事は最終更新日から7年以上が経過しています。

SonarQube で squid:S1162 が発生した時の対処方法

エラー本文

Checked Exception should not be thrown
squid:S1162

    error-handling, security Maintainability > Understandability 

The purpose of checked exceptions is to ensure that errors will be dealt with, either by propagating them or by handling them,
but some believe that checked exceptions negatively impact the readability of source code,
by spreading this error handling/propagation logic everywhere.

This rule verifies that no method throws a new checked exception.

発生原因

可読性が低くなるから検査例外をthrowしてはいけないと指摘されている
発生させている例外が本当に検査例外でなければいけないかを考えてから以下の対応を取る

解消方法

  1. 検査例外を明示的に投げるようなインターフェイスを作成する
  2. そのクラスの実装として検査例外を投げるクラスを作成する

:warning: SonarQubeのJavaプラグインが 3.10 より低いと、この対処を行っても発生する :warning:

Tips

スーパークラスやインターフェイスで強制されていないのに 検査例外を非検査例外に変換すると別のチェックに引っかかる

  • http://fb-contrib.sourceforge.net/bugdescriptions.html
    • EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS

参考

  • https://jira.sonarsource.com/browse/SONARJAVA-1498