JAVA+尝试 catch(FileNotFoundException e) 进入 catch(Exception e) 吗?

发布于 2024-11-04 23:38:24 字数 914 浏览 1 评论 0原文

我有一些在磁盘上创建文件的命令。 因为必须在其中创建文件的文件夹是动态的,所以我有一个 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;
}

I have some command which creates a file on disk.
Because the folder in which the file has to be created is dynamic, I have a catch(FileNotFoundException e). In the same try block, I already have a catch(Exception e) block.
For some reason, when I run my code and the folder does not exists yet, the catch(Exception e) block is used, not the FileNotFoundException one.

The debugger is clear though (to me at least), showing a FileNotFoundException: java.io.FileNotFoundException: c:\mydata\2F8890C2-13B9-4D65-987D-5F447FF0DDA7\filename.png (The system cannot find the path specified)

Any idea why it doesn't go into the FileNotFoundException block?
Thanks;

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡渣熟男 2024-11-11 23:38:24

您遇到的具体问题也可能不是 FileNotFoundException。通过在 catch 块(它是所有异常的父类)中使用“异常”,这实际上是“捕获所有”,因为如果抛出“异常”或其任何子类,它将运行。

尝试以下更改:

...

catch (Exception e) {
  System.out.println(e.getClass());
}
...

这将告诉您该块捕获的异常的特定类。我敢打赌您会发现 Exception 实际上是子类的实例(例如 IOException)。

It's also possible that the specific issue you're having isn't a FileNotFoundException. By using the "Exception" in a catch block (which is the parent class to all Exceptions) this is effectively a "catch all", since it will run if there is an `Exception or any of its subclasses thrown.

Try the following change:

...

catch (Exception e) {
  System.out.println(e.getClass());
}
...

This will tell you the specific class of the Exception being caught by this block. I'll bet you'll find that the Exception is actually an instance of a subclass (such as IOException, for example).

无边思念无边月 2024-11-11 23:38:24

您的问题是 FileNotFoundException 被抛出到 java 库深处的某个地方,并且没有向上传播,因此您无法捕获它。
这里真正的罪魁祸首是源自调用的 NullPointerException

ImageIO.write(image, "png", new File(fileName));

。这会遇到您的 catch (Exception e) 块。
如果您在常规异常捕获之前添加一个 catch (NullPointerException e) 块,您将看到它在那里。

Your problem is that the FileNotFoundException is thrown somewhere deep inside the java library and not propagated up so you cannot catch it.
The real culprit here is a NullPointerException originating from the

ImageIO.write(image, "png", new File(fileName));

call. This one runs into your catch (Exception e) block.
If you add a catch (NullPointerException e) block before your general Exception catch, you will see it going in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文