如何解决'定义并抛出专用异常而不是使用通用异常'.在我的代码中

min*_*nie 5 java exception-handling

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();
}
Run Code Online (Sandbox Code Playgroud)

嗨,这是我的代码,而我检查代码质量,它显示'定义并抛出一个专用的异常,而不是使用通用的"我应该怎么做才能解决这个问题?任何人都可以帮助我.

Kay*_*man 1

你正在抛出一个RuntimeException几乎是你能得到的通用的东西。它告诉你使用更准确地告诉你出了什么问题的东西,定义你自己的StringLengthException(或者你想命名的任何东西)并抛出它。