我已经阅读了很多关于异常的内容,但是我很难将它们拼凑在一起......我正在尝试编写一个实用的方法来读取文件(这是为了练习所以我不感兴趣在使用图书馆):
public static List<String> readFile(String file)
throws NoSuchFileException,
AccessDeniedException,
SomeException1,
SomeException2,
IOException {
Path p = Paths.get(file);
if (!Files.exists(p)) {
throw new NoSuchFileException(file);
} else if (!Files.isRegularFile(p)) {
throw new SomeException1(file);
} else if (!Files.isReadable(p)) {
throw new AccessDeniedException(file);
} else if (Files.size(p) == 0) {
throw new SomeException2(file);
}
return Files.readAllLines(p);
}
public static void main(String[] args) {
try {
if (args.length != 2) {
System.out.println("The proper use is: java MyProgram file1.txt file2.txt");
return;
}
List<List<String>> files = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
try {
files.add(Utilities.readFile(args[i]));
} catch (NoSuchFileException e) {
System.out.printf("File %s does not exist%n", e.getMessage());
System.exit(-1);
} catch (SomeException1 e) {
System.out.printf("File %s is not a regular file%n", e.getMessage());
throw e;
} catch (AccessDeniedException e) {
System.out.printf(
"Access rights are insufficient to read file %s%n", e.getMessage()
);
throw new AccessDeniedException(e);
} catch (SomeException2 e) {
System.out.printf("File %s is empty%n", e.getMessage());
throw new SomeOtherException(e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
我想抛出精确的异常,以便我可以在以后捕获它们并为用户编写相关的错误消息,但这看起来很糟糕......有更好的方法吗?
我看到有几个问题,这是我的想法:
答案 0 :(得分:2)
如果您的异常都来自公共基本异常,例如IOException
,则为基本异常添加throws
声明就足够了:
public static List<String> readFile(String file)
throws IOException {
// ...
}
如果您不想为所有情况创建不同的例外,请使用附加到每个例外的消息:
public static List<String> readFile(String file)
throws IOException {
Path p = Paths.get(file);
if (!Files.exists(p)) {
throw new IOException( "No such file: " + file);
} else if (!Files.isRegularFile(p)) {
throw new IOException( "No regular file: " + file);
} else if (!Files.isReadable(p)) {
throw new IOException( "Access denied: " + file);
} else if (Files.size(p) == 0) {
throw new IOException( "Empty file: " + file);
}
return Files.readAllLines(p);
}
如果您需要为错误处理做的只是打印错误消息,这是一个不错的选择。
答案 1 :(得分:0)
异常可以是对象层次结构的一部分,它允许您详细处理异常或处理您认为方便的任何抽象级别。例如,FileNotFoundException
和CharacterEncodingException
都会延伸IOException
。您可以选择在抽象IOException
级别捕获它们,或者像这样单独处理它们:
catch(FileNotFouncException e){
// handle it specifically
}
catch(CharacterEncodingException e){
// handle it specifically
}
catch(IOException e){
// all others handle the same way
// (Superclass must be handled after subclasses)
}
您还可以将没有方便的公共基类的异常分组,仅仅因为您想以相同的方式处理它们,如下所示:
catch(ZipException|ZipError e){
// deal with corrupt zip file once, even though they come from different class hierarchies
}