场景
我有一个 util 类,它可以从文件夹创建 zip 文件并返回该文件。这很完美。
public static class MakeZip {
// returns zip file, null otherwise
public static File doZip(File folderToZip)
}
问题
当我想控制可能的错误场景、连接问题、空文件夹、zip 失败等时出现...我想知道创建文件并返回的最佳方法结果,为此,我创建了一个枚举
public static enum ZIP_RESULT {
OK, KO, EMPTY_FOLDER;
}
想法
a) 创建文件类属性
private File zipFile;
并在 doZip 方法中返回 ZIP_FILE
枚举。
public ZIP_RESULT doZip(File folderToZip)
b)修改发送的文件
:
public ZIP_RESULT doZip(File folderToZip, File fileToZip)
c) ????
问题
对于这种期望枚举结果的文件修改是否有模式、约定或最佳实践?我想这是一个非常典型的场景......
最佳答案
抛出异常。您可以扩展Exception
或实现Throwable
。
关于何时使用异常的第三个项目描述了您的情况,如here所述。 :
Exceptions due to resource failures: Exceptions that get generated when resources fail. For example: the system runs out of memory or a network connection fails. The client's response to resource failures is context-driven. The client can retry the operation after some time or just log the resource failure and bring the application to a halt.
关于java - 创建文件并返回枚举作为结果的模式或约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192828/