gpt4 book ai didi

java - 使用枚举来清晰地表示错误消息——这是好的做法吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:28:07 32 4
gpt4 key购买 nike

我想将我的错误消息和内容合并到一个文件中,并尽可能使我的代码更具可读性。

这是我的枚举文件中的示例:

public enum ZipErrorType {

// START: define exception messages (alphabetical order)
EMPTY_FILE_NAME_IN_LIST {
public String toString() {
return "One or more null/empty filename(s) found";
}
},

FILE_DOESNT_EXIST {
public String who(String sThisFile) {
return "[" + sThisFile + "] does not exist";
}
},

FILE_LIST_IS_NULL {
public String toString() {
return "File list is null/empty";
}
},

FILENAME_NOT_ABSOLUTE {
public String who(String sThisFile) {
return "[" + sThisFile + "] is not absolute";
}
},

MUST_BE_DIR {
public String who(String sThisFile) {
return "[" + sThisFile + "] must be a directory";
}
},

MUST_BE_FILE {
public String who(String sThisFile) {
return "[" + sThisFile + "] must be a file";
}
},

NULL_OR_EMPTY {
public String who(String sThisFile) {
return "[" + sThisFile + "] is null/empty";
}
},

OUTPUT_FILE_ALREADY_EXISTS {
public String who(String sThisFile) {
return "[" + sThisFile + "] already exists";
}
},

OUTPUT_FILENAME_EMPTY {
public String toString() {
return "Output filename is null/empty";
}
},

OUTPUT_PATH_EMPTY {
public String toString() {
return "Output path is null/empty";
}
},
// END: define exception messages

NONE {};

public String who(String sThisFile) { return ""; }
}

然后在我的程序中我有这样的代码:

private static ZipErrorType getFileErrorsIfAny(String sFilename, boolean shouldBeFile) {

// check if given filename is absolute
File file = new File(sFilename);
if (!file.isAbsolute()) {
return ZipErrorType.FILENAME_NOT_ABSOLUTE;
}

// check if file exists
if (!file.exists()) {
return ZipErrorType.FILE_DOESNT_EXIST;
}

// check if corresponding file is a file when it shouldn't be...
if (file.isFile() && !shouldBeFile) {
return ZipErrorType.MUST_BE_DIR;
}
// ...or a directory when it should be a file
else if (file.isDirectory() && shouldBeFile) {
return ZipErrorType.MUST_BE_FILE;
}

return ZipErrorType.NONE;
}

...以及我如何使用枚举的示例:

    // check input files
for (String sFile : files) {
if (sFile == null || sFile.trim().length() == 0) {
throw new NullPointerException("One or more filename is null/empty");
}

errorIfAny = getFileErrorsIfAny(sFile.trim(), true);
if (!errorIfAny.equals(ZipErrorType.NONE)) {
throw new ZipInputException(errorIfAny.who(sFile.trim()));
}
}

现在我知道仅凭这些代码片段很难判断,但从一般角度来看,这样可以吗?我正在做的事情不值得麻烦吗?有什么方法可以改进吗?

最佳答案

我建议使用简单的字符串模板而不是枚举来构建错误消息。像这样:

String EMPTY_FILE_NAME_IN_LIST_TEMPLATE = "One or more null/empty filename(s) found";
String FILE_DOESNT_EXIST_TEMPLATE = "[ %s ] does not exist";
String FILE_LIST_IS_NULL_TEMPLATE = "File list is null/empty";
String FILENAME_NOT_ABSOLUTE_TEMPLATE = "[ %s ] is not absolute";
String MUST_BE_DIR_TEMPLATE = "[ %s ] must be a directory";
String MUST_BE_FILE_TEMPLATE = "[ %s ] must be a file";
String NULL_OR_EMPTY_TEMPLATE = "[ %s ] is null/empty";
String OUTPUT_FILE_ALREADY_EXISTS_TEMPLATE = "[ %s ] already exists";
String OUTPUT_FILENAME_EMPTY_TEMPLATE = "Output filename is null/empty";
String OUTPUT_PATH_EMPTY_TEMPLATE = "Output path is null/empty";

然后,使用 String.format(template, sFilename) 构建实际消息。

您还可以考虑直接从 getFileErrorsIfAny() 方法中抛出异常:

File file = new File(sFilename);
if (!file.isAbsolute()) {
throw new ZipInputException(String.format(FILENAME_NOT_ABSOLUTE_TEMPLATE, sFilename));
}

对我来说看起来更干净、更紧凑。

关于java - 使用枚举来清晰地表示错误消息——这是好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16009715/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com