无法使try-catch-finally块正常工作
问题描述:
为了让try-catch-finally块正常工作,我苦苦挣扎了很长时间。我是一名java初学者,目前正在学习如何读/写/处理异常。在我的任务中,我试图从两个单独的.txt文件中读取。一个有国家和人口,另一个有国家和地区。这将进一步打印到一个新文件,其中显示有关每个国家和地区的信息。无法使try-catch-finally块正常工作
我不确定如果我真的可以把终于在try-catch块内。 目前我收到错误消息“未处理的FileNotFoundException等。”。我一直在尝试这个很长一段时间,只是不能让它正常工作。
private String country;
private double value;
Scanner in1 = new Scanner(new File("countryPopulation.txt"));
Scanner in2 = new Scanner(new File("countryArea.txt"));
PrintWriter out = new PrintWriter("countryAreaPerInhabitant");
public IOAndExceptionHandling(String line) {
int i = 0;
while (!Character.isDigit(line.charAt(i))) {
i++;
}
this.country = line.substring(0, i - 1).trim();
this.value = Double.parseDouble(line.substring(i).trim());
}
public String getCountry() {
return this.country;
}
public double getValue() {
return this.value;
}
public void printAreaPerPerson() {
try {
try {
while (in1.hasNextLine() && in2.hasNextLine()) {
IOAndExceptionHandling country1 = new IOAndExceptionHandling(in1.nextLine());
IOAndExceptionHandling country2 = new IOAndExceptionHandling(in1.nextLine());
double density = 0;
if (country1.getCountry() == country2.getCountry()) {
density = country2.getValue()/country1.getValue();
out.println(country1.getCountry() + " : " + density);
}
}
}
finally {
in1.close();
in2.close();
out.close();
}
}
catch (FileNotFoundException f) {
System.out.println("FileNotFound!");
}
catch (IOException e) {
e.printStackTrace();
}
}
谢谢! :)
答
终止块追赶catch块。无论是抛出异常还是成功完成块,它都会执行。
Scanner in1; //field declaration with no assignment
Scanner in2; //field declaration with no assignmetn
/* Omitted Class declaration & other code */
try {
in1 = new Scanner(new File("countryPopulation.txt")); //these require FNF to be caught
in2 = new Scanner(new File("countryArea.txt"));
while (in1.hasNextLine() && in2.hasNextLine()) {
IOAndExceptionHandling country1 = new IOAndExceptionHandling(
in1.nextLine());
IOAndExceptionHandling country2 = new IOAndExceptionHandling(
in1.nextLine());
double density = 0;
if (country1.getCountry() == country2.getCountry()) {
density = country2.getValue()/country1.getValue();
out.println(country1.getCountry() + " : " + density);
}
}
} catch (FileNotFoundException f) {
System.out.println("FileNotFound!");
} catch (IOException e) {
e.printStackTrace();
} finally {
in1.close();
in2.close();
out.close();
}
这段代码的第一个节目是抛出未处理的异常错误,因为内部的try块没赶上FileNotFoundException
。尽管你有一个try ... catch包装,它试图阻止FileNotFoundException
,但这个例外不会通过嵌套try..catch语句向上传播
答
您正在嵌套两个try catch
块。内部只有try finally
,但没有catch
报表。这就是FileNotFoundException
会发生的地方。
try {
try {
要么除去外部块和只使用一个或移动内部try finally
内的catch
语句。
复制粘贴此
public void printAreaPerPerson() {
try {
while (in1.hasNextLine() && in2.hasNextLine()) {
IOAndExceptionHandling country1 = new IOAndExceptionHandling(in1.nextLine());
IOAndExceptionHandling country2 = new IOAndExceptionHandling(in1.nextLine());
double density = 0;
if (country1.getCountry() == country2.getCountry()) {
density = country2.getValue()/country1.getValue();
out.println(country1.getCountry() + " : " + density);
}
}
} catch (FileNotFoundException f) {
System.out.println("FileNotFound!");
} catch (IOException e) {
e.printStackTrace();
} finally {
in1.close();
in2.close();
out.close();
}
}
答
把你的try块的finally块出方。 你不需要内部的尝试块。
为什么要试试? – 2013-05-14 09:30:01