Java – how to solve ‘define and throw special exceptions instead of using general exceptions.’
•
Java
When the length of the two lists is not equal, I need to throw runtimeException We use the sonarqube tool for code review
This 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, sonarqube raises the problem of defining and throwing special exceptions instead of using general exceptions Throw a new runtimeException line I don't know which example I can replace to solve the sonarqube problem
Solution
If these two lists are parameters passed to the method, illegalargumentexception will be a good candidate It is a subclass of runtimeException, so you will still throw a runtimeException
if (objctArray.length != columnArray.length) { throw new IllegalArgumentException(String.format("objctArray and columnArray length is not same. objctArray length = %d,columnArray.length)); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码