try{
IMAGE = ImageIO.read(getClass().getResource("Images/image.png"));
}
catch (IOException ex){
JOptionPane.showMessageDialog(null, "<html>Error<br>Missing images</html>" ,"Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
catch块无效,我仍然收到默认消息:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at KPK.<init>(KPK.java:40)
at Main.main(Main.java:22)
我怎样才能抓住这个例外?
答案 0 :(得分:3)
IllegalArgumentException
。此错误表明您的代码中存在非常糟糕的情况,并且不应该被捕获。请参阅Jon Skeet的回答here,了解更多关于为何这样做的原因。相反,你应该:getResource()
ImageIO.read(...)
调用它。即,
try{
URL imgUrl = getClass().getResource(IMAGE_PATH); // path should be a constant
// or variable, not a String literal
if (imgUrl == null) {
// show error and get the heck out of here
} else {
image = ImageIO.read(imgUrl);
}
} catch (IOException ex){
JOptionPane.showMessageDialog(null, "<html>Error<br>Missing images</html>" ,
"Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
答案 1 :(得分:-1)
catch (IOException | IllegalArgumentException ex)
此外,首先创建图片... Image im = null;
然后使用文件... im = ImageIO.read(new File("YOUR IMAGE FILE PATH"));