【发布时间】:2021-11-12 15:23:18
【问题描述】:
我正在编写一个程序来练习抛出异常,并在其中一个测试中得到编译错误:
MyTest.java:29:错误:未报告的异常 FileNotFoundException;必须被抓住或宣布被抛出:
Assert.assertEquals("找不到文件", task2.fileNotFoundExTest());
^
public void fileNotFoundEx() throws FileNotFoundException {
File fileName = new File("foo.txt");
BufferedReader rd = new BufferedReader(new FileReader(fileName));
}
public String fileNotFoundExTest() throws FileNotFoundException {
try {
fileNotFoundEx();
} catch (FileNotFoundException e) {
return "File Not Found";
}
return "No Error";
}
我不知道为什么会出现这个错误,因为我已经将导致异常的方法放在了 try-catch 块中,并为这两个方法添加了签名“throws FileNotFoundException”。谁能解释一下?
【问题讨论】: