error while using try with resources in Java

error while using try with resources in Java



I have this method where I am using try with resources of Java SE 7.


private void generateSecretWord(String filename)

try (FileReader files = new FileReader(filename))
Scanner input = new Scanner(files);
String line = input.nextLine();
String words = line.split(",");
Collections.shuffle(Arrays.asList(words));
if (words[0].length()>1)
secretWord = words[0];
return;


catch (FileNotFoundException e)
e.printStackTrace();

finally
if (files!=null) files.close();





I get compile error in finally block that files cannot be resolved to a variable
I have reference for files in the try with block. why do I get this error and how to fix it?


finally


files cannot be resolved to a variable


try with block



Thanks




4 Answers
4



Taken from the Java Language Spec (14.20.3):



A try-with-resources statement is parameterized with variables (known as resources) that are initialized before execution of the try block and closed automatically, in the reverse order from which they were initialized, after execution of the try block. catch clauses and a finally clause are often unnecessary when resources are closed automatically.



A ResourceSpecification declares one or more local variables with initializer expressions to act as resources for the try statement.



So you do not need to close the Resource anymore. Try-with-resources does it automatically for you and your FileReader will only be available in the try block. Thus you get that compile error.


FileReader


try





does that mean a finally block is not needed or just closing files is not needed
– eagertoLearn
Nov 22 '13 at 21:07


finally


just closing files





@user2708477 It means that a finally block is not needed to close the Resources. If you need to do other things, which cannot be skipped even if an Exception happens, then you will still need a finally block. But if you just want to make sure that the files will be closed in any case, you don't need one, because that will be done implicitly.
– noone
Nov 22 '13 at 21:09



finally


Exception


finally



When you are using try with resources you don't need to explicitly close them. try-with-resources will take care of closing those resources.



Based on try-wtih-resource document



The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.



Since no-one else has mentioned this, if you want to handle it manually you could do something like:


private void generateSecretWord(String filename)
FileReader files = null;
try
files = new FileReader(filename);
Scanner input = new Scanner(files);
String line = input.nextLine();
String words = line.split(",");
Collections.shuffle(Arrays.asList(words));
if (words[0].length()>1)
secretWord = words[0];
return;


catch (FileNotFoundException e)
e.printStackTrace();

finally
if (files!=null)
files.close();





The code that you are trying to execute is old fashioned code before Java 7 where have to close the resources to avoid memory leaks. But in New Java 7 take it smartly and it is not necessary to close the resources even it is not accessible to finally block.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế