我有一些命令可以在磁盘上创建一个文件。因为必须在其中创建文件的文件夹是动态的,所以我有一个 catch(FileNotFoundException e)。在同一个 try 块中,我已经有一个 catch(Exception e) 块。出于某种原因,当我运行我的代码并且该文件夹还不存在时,使用了 catch(Exception e) 块,而不是 FileNotFoundException 块。
调试器很清楚(至少对我来说),显示 FileNotFoundException: java.io.FileNotFoundException: c:\mydata\2F8890C2-13B9-4D65-987D-5F447FF0DDA7\filename.png (系统找不到指定的路径)
知道为什么它不进入 FileNotFoundException 块吗?谢谢;
代码:
import java.io.FileNotFoundException;
try{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}
catch (FileNotFoundException e){
// do stuff here..
return false;
}
catch(Exception e){
// do stuff here..
return = false;
}