只是一个简单的表格问题。在下面的代码中,是否有更好的方法来隐藏字符串(即,我可以将tmpError设置为等于新字符串而不是添加它吗?)

public void validate () throws Exception {
    String tmpError = "";
    if(paramA == null) tmpError = tmpError + "paramA was not set";

    if(paramB == null) tmpError = tmpError + "paramB was not set";

    if(paramC == null) tmpError = tmpError + "paramC was not set";


    if(!tmpError.equalsIgnoreCase("")){
        tmpError = "error occured" + tmpError;
        throw new Exception(tmpError);
    }
}


提前致谢

最佳答案

我总是建议使用StringBuilder

像这样的东西:

public void validate() throws Exception {
    StringBuilder error = new StringBuilder();
    if(paramA == null)
        error.append("paramA was not set");

    if(paramB == null)
        error.append("paramB was not set");

    if(paramC == null)
        error.append("paramC was not set");


    if(error.length() > 0) {

        throw new Exception("error occured " + error.toString());
    }
}

10-08 16:14