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

String replaceSubString(String orignal, int startIndex, int endIndex, String replaceWith){

    StringBuffer buffer = new StringBuffer(orignal);

    if ((action.getEndindex() - action.getStartindex()) < 0) {
        if (replaceWith.length() > (startIndex - endIndex)) {

                throw new RuntimeException("Replace string lenght not same as difference of start & end index.");

        }
        buffer = buffer.replace(endIndex, startIndex, replaceWith);
    } else {
        if (replaceWith.length() > (endIndex - startIndex)) {

                throw new RuntimeException("Replace string lenght not same as difference of start & end index.");

        }
        buffer = buffer.replace(startIndex, endIndex, replaceWith);
    }
    return buffer.toString();
}

Hi, This is my code, while I test it for the quality of the code, it shows “Detect and throw a highlighted exception instead of using a generic”, what should I do to solve this problem? Can anyone help me with this.

+4
source share
3 answers

You drop RuntimeException, which pretty much resembles a generic one. This tells you to use something that more accurately says what went wrong by defining your own StringLengthException(or whatever you want to name), and drop it instead.

+1
source

, RuntimeException, , IllegalArgumentException ( , ) IndexOutOfBoundsException ( , ).

+1

The quality of the code asks you to define your own exception instead of using the predefined java exception class.

So, you must declare a new class (e.g. ReplaceStringLengthException) inherited from RuntimeException, or you can throw any exception that is thrown from a RuntimeException.

0
source

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


All Articles