【问题标题】:Can't get the try-catch-finally block to work无法让 try-catch-finally 块工作
【发布时间】:2013-05-14 09:27:05
【问题描述】:

我一直在努力让 try-catch-finally 块工作。我是java初学者,目前正在学习如何读/写/处理异常。在我的任务中,我试图从两个单独的 .txt 文件中读取。一个有国家和人口,另一个有国家和地区。这将进一步打印到一个新文件中,其中显示有关每个人的国家和地区的信息。

我不确定我是否真的可以将 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();
    }
}

谢谢! :)

【问题讨论】:

    标签: java io try-catch finally


    【解决方案1】:

    finally 块在 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 包装了该 try 块,它确实捕获了 FileNotFoundException,但异常不会通过嵌套的 try..catch 语句向上传播

    【讨论】:

    • 好的,这看起来应该可以工作,但是当我在我的代码中尝试这个时,我仍然收到相同的错误消息。
    • @martyft 很高兴我能帮上忙。
    【解决方案2】:

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

    try {   
            try {
    

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

    复制粘贴此

    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();
        }  
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2012-11-12
        • 2013-12-16
        • 2015-07-11
        • 2014-08-09
        • 2011-06-01
        • 2012-05-26
        • 2015-09-05
        • 2014-11-27
        • 2011-08-31
        相关资源
        最近更新 更多