如何捕获“从不抛出”的异常爪哇语
我有以下代码块,它使用 http://www.jcraft.com/jsch 中找到的 JSCH 库/
try {
channel.put(f, filename);
} catch (FileNotFoundException e) {
System.out.println("no file.");
}
我知道当本地找不到 f 指定的文件时,put 方法可以抛出 FileNotFoundException,但是 eclipse 告诉我 catch 块无法访问,并且永远无法抛出该异常。当我更改为:
try {
channel.put(f, filename);
} catch (Exception e) {
System.out.println(e.getMessage());
}
我得到:
java.io.FileNotFoundException: C:\yo\hello2 (The system cannot find the file specified)
有什么想法吗?
I have the following block of code, which uses the JSCH library found at http://www.jcraft.com/jsch/
try {
channel.put(f, filename);
} catch (FileNotFoundException e) {
System.out.println("no file.");
}
I know that the put method can throw a FileNotFoundException when the file specified by f is not found locally, but eclipse tells me that the catch block is unreachable, and that exception can never be thrown. When I change to:
try {
channel.put(f, filename);
} catch (Exception e) {
System.out.println(e.getMessage());
}
I get:
java.io.FileNotFoundException: C:\yo\hello2 (The system cannot find the file specified)
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的
FileNotFoundException
被包装在channel
方法抛出的另一个异常中,因此您无法捕获它。尝试打印该方法抛出的异常的类:
I think your
FileNotFoundException
is wrapped in another thrown by thechannel
method and therefor you cannot catch it.Try printing the class of the exception thrown by the method:
检查您的导入语句,确保您没有从
java.io
之外的包中导入FileNotFoundException
类。Check your import statements to ensure you are not importing a
FileNotFoundException
class from a package besidesjava.io
.