How to resolve "Define and throw a highlighted exception instead of using a common one."

I need it throw RuntimeExceptionwhen the length of two lists is not equal. We use a SonarQubecode verification tool.

Here is the code:

if (objctArray.length != columnArray.length) {
                throw new RuntimeException(String.format("objctArray and columnArray length is not same. objctArray length = %d, columnArray length = %d", objctArray.length, columnArray.length));
            }

Now SonarQubecauses a problem with Define and throw a dedicated exception instead of using a generic one.in line throw new RuntimeException. I do not know what exception I can replace to solve the problem SonarQube.

+4
source share
2 answers

If the two lists are arguments passed to the method, it IllegalArgumentExceptionwill be a good candidate for a throw. This is a subclass of the class RuntimeException, so you will still throw a kind RuntimeException.

if (objctArray.length != columnArray.length) {
    throw new IllegalArgumentException(String.format("objctArray and columnArray length is not same. objctArray length = %d, columnArray length = %d", objctArray.length, columnArray.length));
}
+7
source

. Exception , , , .

+4

Source: https://habr.com/ru/post/1629855/


All Articles