Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: finally clause

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default finally clause

    Hey guys, I have a few doubts about the finally clause in exception handling.

    What exactly does it do? Does it need to have try and catch blocks before it in order to work?

    I just can't seem to understand the use of it or a good example.

    Thanks a lot.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: finally clause

    Finally is generally used for clean-up code you want to run irregardless of if an exception is thrown or not, for example closing IO streams. You do need a try block, but you don't need any catch clauses to have a finally block (this allows exceptions to propagate while still running cleanup code).

    void readFile(BufferedReader reader) throws IOException
    {
    	try
    	{
    		// read data
    	}
    	finally
    	{
    		System.out.println("done reading data");
    		reader.close();
    	}
    }

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: finally clause

    What about that variable "reader" ? What can I do with it?

Similar Threads

  1. Finally...
    By newbie in forum The Cafe
    Replies: 2
    Last Post: September 26th, 2012, 11:14 AM