Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: finally keyword and auto variables

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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:

    Java Code:
    try
    {
    	FileReader fil = new FileReader("C:\\hello.txt");
    	BufferedReader fi = new BufferedReader (fil);
    	//Code
    }
    catch
    {
    	//Code
    }
    finally
    {
    	fi.close ();
    }
    But we could also write like this:

    Java Code:
    FileReader fil;
    BufferedReader fi;
    try
    {
    	fil = new FileReader("C:\\hello.txt");
    	fi = new BufferedReader (fil);
    	//Code
    }
    catch
    {
    	//Code
    }
    fi.close ();
    I prefer the second way.
    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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Pojahn_M View Post
    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:

    Java Code:
    try
    {
    	FileReader fil = new FileReader("C:\\hello.txt");
    	BufferedReader fi = new BufferedReader (fil);
    	//Code
    }
    catch
    {
    	//Code
    }
    finally
    {
    	fi.close ();
    }
    But we could also write like this:

    Java Code:
    FileReader fil;
    BufferedReader fi;
    try
    {
    	fil = new FileReader("C:\\hello.txt");
    	fi = new BufferedReader (fil);
    	//Code
    }
    catch
    {
    	//Code
    }
    fi.close ();
    I prefer the second way.
    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.
    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,

    Jos
    JeffGrigg likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,965
    Rep Power
    19

    Default

    In the second case, if an exception is thrown, how will the file reader stream (fil) be closed?

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,965
    Rep Power
    19

    Default

    Oh, and why would auto be any use at all in Java?

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,965
    Rep Power
    19

    Default

    ...and why did I not notice that Jos had already replied?

  6. #6
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    I am not catching your point. Can you write a simple code that explain how its useful?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    public void someMethod(){
      BufferedReader br = ...;
      try{
        do something that can throw exceptions
      } catch(NumberFormatException nfe){
        nfe.printStackTrace();
      }
      close reader outside finally
    }
    If an exception is thrown how does the file get closed?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    ...and why did I not notice that Jos had already replied?
    Invisible ink.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. The Mystery of the auto-morphing variables
    By Luxpsycho in forum Advanced Java
    Replies: 3
    Last Post: 08-11-2011, 12:06 PM
  2. Replies: 2
    Last Post: 06-03-2011, 09:15 PM
  3. Finally...
    By leke in forum Introductions
    Replies: 1
    Last Post: 08-23-2010, 06:46 PM
  4. Auto contrast and auto brightness
    By oxxxis in forum Java 2D
    Replies: 0
    Last Post: 01-21-2010, 08:32 PM
  5. Auto-complete/Auto-fix for custom statement
    By dark_cybernetics in forum Eclipse
    Replies: 0
    Last Post: 08-19-2008, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •