java 未报告的异常必须被捕获或声明为抛出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13998815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unreported exception must be caught or declared to be thrown
提问by user1892955
Possible Duplicate:
Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
Thanks guy for solving my first issue, i am now getting a new error
感谢家伙解决了我的第一个问题,我现在遇到了一个新错误
import java.io.*;
import javax.swing.*;
public class FileBrowser {
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
String filename = file.getName();
System.out.println("You have selected: " + filename);
FileReader fr = new FileReader("filename");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
}
}
Error :
错误 :
java.io.FileNotFoundException: filename (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at java.io.FileReader.<init>(FileReader.java:58)
at FileBrowser.main(FileBrowser.java:13)
is the error where it is not properly getting the file name from the file browser ?
是不是无法从文件浏览器正确获取文件名的错误?
回答by Mark Byers
There are checked and unchecked exceptions in Java. Checked exceptions that your method throws must be declared. The FileReader
constructorcan throw a FileNotFoundException
which is a checked exception. Some of the other method calls in your code also can throw checked exceptions.
Java 中有已检查和未检查的异常。必须声明您的方法抛出的已检查异常。该FileReader
构造函数可以抛出一个FileNotFoundException
是经过检查的异常。代码中的一些其他方法调用也可能抛出已检查的异常。
If you call a method that can throw a checked exception then you either need to catch the exception and handle it or declare that your method throws this exception. You have to do one of these even if you believe that the exception will never be thrown. Failure to do so is a compile error.
如果您调用一个可以抛出已检查异常的方法,那么您要么需要捕获该异常并对其进行处理,要么声明您的方法抛出此异常。即使您相信永远不会抛出异常,您也必须执行其中之一。不这样做是编译错误。
Either add a throws
:
添加一个throws
:
public static void main(String[] args) throws IOException {
or surround the code with a try/catch block:
或用 try/catch 块包围代码:
try {
// ...
} catch (IOException e) {
e.printStackTrace();
// Or ask the user for a different filename...
}
回答by Adel Boutros
surround FileReader fr = new FileReader("filename")
; with try and catch as follow:
环绕FileReader fr = new FileReader("filename")
;尝试和捕获如下:
try {
FileReader fr = new FileReader("filename");
} catch (Exception e) {
System.out.println("Error: " + e);
}
回答by Smit
In addition to Adel Boutrons and Mark Byersanswers. You also need to make some changes.
除了Adel Boutrons 和 Mark Byers 的回答。您还需要进行一些更改。
Firstly You are choosing file chooser so you will also need
首先,您正在选择文件选择器,因此您还需要
String path = file.getAbsolutePath();
This will give absolute path of your file including your file name.
这将给出您的文件的绝对路径,包括您的文件名。
FileReader fr = new FileReader("filename");
Here you are not giving any file name, just string, remove "filename".
在这里你没有给出任何文件名,只是字符串,删除“文件名”。
FileReader fr = new FileReader(path.replace("\", File.separator));
If file not found then it will throw FileNotFoundException.
如果未找到文件,则会抛出 FileNotFoundException。