这是一本书中的练习代码,我正在学习 try/catch 语句。
显示的第一个方法调用加载方法并检索两个图像。此代码中的错误是资源文件中第一个 img 的名称称为“welcome.png”,但如您所见,它显示为“welcomee.png”(欢迎末尾的额外 e)。当我运行代码时,它不会打印出 catch 语句中的代码。它确实显示了堆栈跟踪(因为无论如何它都会这样做),但它不会打印出“读取时出错:文件名”。这是为什么?
public class Resources {
public static BufferedImage welcome, iconimage;
public static void load() {
welcome = loadImage("welcomee.png");
iconimage = loadImage("iconimage.png");
}
private static AudioClip loadSound(String filename) {
URL fileURL = Resources.class.getResource("/resources/" + filename);
return Applet.newAudioClip(fileURL);
}
private static BufferedImage loadImage(String filename) {
BufferedImage img = null;
try {
img = ImageIO.read(Resources.class.getResourceAsStream("/resources/" + filename));
} catch (IOException e) {
System.out.println("Error while reading: " + filename);
e.printStackTrace();
}
return img;
}
}
最佳答案
您正在捕获一个IOException
,但是Class.getResourceAsStream
如果未找到资源,则不会抛出 IOException
;它只返回空值。和 ImageIO.read
如果参数为 null,则不会抛出 IOException
;它抛出一个 IllegalArgumentException
。
我建议您以这种方式重构代码:
private static BufferedImage loadImage(String filename) {
try (InputStream in = Resources.class.getResourceAsStream("/resources/" + filename)) {
if (in == null) throw new IOException("Resource not found");
return ImageIO.read(in);
} catch (IOException e) {
System.out.println("Error while reading: " + filename);
e.printStackTrace();
return null;
}
}
这将确保您处理错误,并使用 try-with-resources语句确保流在使用后始终关闭(ImageIO.read
不会这样做)。直接的 return
语句可能更简洁一些,因为它避免了对 img
变量的需要。
您还应该考虑向调用者抛出(或重新抛出)一些异常,例如 UncheckedIOException
,而不是在错误时返回 null ,因为 null 返回值只会在您尝试使用它时在其他地方引起错误,并且从问题点直接抛出的异常将比稍后抛出的某些 NullPointerException
更有意义。
关于java - 为什么 catch 语句在这里被忽略了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29886224/