我有一些命令,它在磁盘上创建一个文件。 因为要创建文件的文件夹是动态的,所以我有一个catch(FileNotFoundException e)。在同一个try块中,我已经有一个catch(Exception e)块。 出于某种原因,当我运行我的代码并且文件夹尚不存在时,使用catch(Exception e)块,而不是FileNotFoundException。JAVA + try catch(FileNotFoundException e)进入catch(异常e)?
调试器是明确的,但(至少对我来说),呈现出FileNotFoundException异常:java.io.FileNotFoundException:C:\ MYDATA \ 2F8890C2-13B9-4D65-987D-5F447FF0DDA7 \ filename.png(系统无法找到指定的路径)
任何想法为什么它没有进入FileNotFoundException块? 谢谢;
CODE:
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;
}
如果您发布实际存在问题的代码段,这将会很有帮助。另外请确保您正在导入的FileNotFoundException与正在引发的FileNotFoundException相同。偶尔你的IDE可能会导入错误的软件包,如果你的类路径中还有一个FileNotFoundException异常的库。不太可能,但首先排除这一点很重要。 – Jberg 2011-05-05 14:05:54
请张贴代码和stacktrace以及。 – Bhushan 2011-05-05 14:29:36
这应该起作用。使用@normalocity建议的确认异常类名称。我敢冒险猜测它可能是一个普通的IOException。 – JustinKSU 2011-05-05 15:51:54