Results 1 to 8 of 8
- 08-22-2011, 03:46 PM #1
finally keyword and auto variables
First question, is finally keyword useless? I know what it does but I can not think of any use for it.
Example, we could write like this:
But we could also write like this:Java Code:try { FileReader fil = new FileReader("C:\\hello.txt"); BufferedReader fi = new BufferedReader (fil); //Code } catch { //Code } finally { fi.close (); }
I prefer the second way.Java Code:FileReader fil; BufferedReader fi; try { fil = new FileReader("C:\\hello.txt"); fi = new BufferedReader (fil); //Code } catch { //Code } fi.close ();
So, is it useless? And is there an other way to use keyword finally without it being related to try-catch?
Second, am I the only one who want auto keyword in Java? I think it could be useful.
- 08-22-2011, 03:52 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
But what would your code do if it doesn't catch a certain Exception type? The part of your code following the catch clause(s) won't be reached and you won't be able to close your resource(s) or do any other cleanup work ...
The 'auto' keyword (as in C) is a relic; local variables are 'auto' by themselves.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-22-2011, 03:53 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
In the second case, if an exception is thrown, how will the file reader stream (fil) be closed?
- 08-22-2011, 03:55 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Oh, and why would auto be any use at all in Java?
- 08-22-2011, 03:57 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
...and why did I not notice that Jos had already replied?
- 08-23-2011, 12:08 AM #6
- 08-23-2011, 06:53 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If an exception is thrown how does the file get closed?Java Code:public void someMethod(){ BufferedReader br = ...; try{ do something that can throw exceptions } catch(NumberFormatException nfe){ nfe.printStackTrace(); } close reader outside finally }
- 08-23-2011, 07:28 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
The Mystery of the auto-morphing variables
By Luxpsycho in forum Advanced JavaReplies: 3Last Post: 08-11-2011, 12:06 PM -
Can you use the super keyword when variables in parent class are private?
By lam5442 in forum New To JavaReplies: 2Last Post: 06-03-2011, 09:15 PM -
Finally...
By leke in forum IntroductionsReplies: 1Last Post: 08-23-2010, 06:46 PM -
Auto contrast and auto brightness
By oxxxis in forum Java 2DReplies: 0Last Post: 01-21-2010, 08:32 PM -
Auto-complete/Auto-fix for custom statement
By dark_cybernetics in forum EclipseReplies: 0Last Post: 08-19-2008, 11:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks