Question

I have struggled a long time to get the try-catch-finally block to work. I'm a beginner in java, and are currently learning how to read/write/handle exceptions. In my task I'm trying to read from two separate .txt files. One has countries and population, the other has countries and the area of the country. This is further printed out to a new file where information about countries and area per person is displayed.

I'm not sure if I really can put the finally inside a try-catch block. Currently I'm getting the error message "Unhandled FileNotFoundException etc.". I've been trying this for for a long time now, and just can't get it to work properly.

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

Thanks! :)

Was it helpful?

Solution

The finally block goes after the catch blocks. It will execute regardless of an exception being thrown or successful completion of the block.

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

The first rendition of this code was throwing the unhandled exception error because the inner try block did not catch the FileNotFoundException. Even though you had a try...catch wrapping that try block, which did catch the FileNotFoundException, the exception would not propogate upwards through the nested try..catch statements

OTHER TIPS

You are nesting two try catch blocks. The inner one only has try finally, but no catch statements. That's where the FileNotFoundException would occur.

try {   
        try {

Either remove the outer block and just use one or move the catch statements inside the inner try finally.

Copy paste this

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

Put the finally block out side of your try block. You don't need the inner try block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top