Cannot get try-catch-finally block to work

For a long time I tried to occupy the try-catch-finally block. I am new to java and am currently learning how to read / write / handle exceptions. In my task, I am trying to read from two separate .txt files. One has countries and a population, while others have countries and a region of the country. This is additionally printed in a new file, which displays information about countries and regions per person.

I'm not sure if I can finally insert a try-catch block. I am currently receiving the error message "Unhandled FileNotFoundException etc.". I tried this for a long time, and just can't get it to work correctly.

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!:)

+4
source share
3 answers

The finally block comes after the catch blocks. It will be executed regardless of which exception is 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 execution of this code threw an unhandled exception error because the internal try block did not catch a FileNotFoundException . Despite the fact that you had an attempt ... catch wrapping that tried to block that broke FileNotFoundException , the exception would not propagate upward through the nested try..catch statements

+4
source

Nested two try catch blocks. Internal has only try finally , but not catch . That a FileNotFoundException will FileNotFoundException .

 try { try { 

Remove the outer block and just use it or move the catch statements inside the internal try finally .

Copy this link

 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
source

Place the final block on the side of the try block. You do not need an internal try block.

0
source

Source: https://habr.com/ru/post/1480670/


All Articles