首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使try-catch-finally块工作

无法使try-catch-finally块工作
EN

Stack Overflow用户
提问于 2013-05-14 17:27:06
回答 3查看 1.5K关注 0票数 4

我花了很长时间才让try-catch-finally代码块工作。我是java的初学者,目前正在学习如何读/写/处理异常。在我的任务中,我尝试从两个独立的.txt文件中读取数据。一个是国家和人口,另一个是国家和国家面积。这将进一步打印到一个新文件中,其中显示了有关国家和人均面积的信息。

我不确定我是否真的可以把finally放在try-catch块中。目前,我收到错误消息"Unhandled etc.“。我已经尝试了很长一段时间,就是不能正常工作。

代码语言:javascript
复制
    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();
    }
}

谢谢!:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-14 17:29:30

finally块位于catch块之后。无论抛出异常或成功完成块,它都将执行。

代码语言:javascript
复制
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块try...catch,它捕获了FileNotFoundException,异常也不会通过嵌套的try..catch语句向上传播

票数 4
EN

Stack Overflow用户

发布于 2013-05-14 17:29:43

您正在嵌套两个try catch块。内部的只有try finally,没有catch语句。这就是FileNotFoundException发生的地方。

代码语言:javascript
复制
try {   
        try {

要么删除外部块,只使用一个块,要么将catch语句移到内部try finally中。

复制粘贴此

代码语言:javascript
复制
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();
    }  
}
票数 2
EN

Stack Overflow用户

发布于 2013-05-14 17:33:18

将finally块放在try块的外部。您不需要内部try块。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16539539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档