Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unreported Exceptions and Closing Objects 1

Status
Not open for further replies.

fatcodeguy

Programmer
Joined
Feb 25, 2002
Messages
281
Location
CA
Hi,

I have a program in which I do a lot of file reading, so I want to ensure i always close my FileInputStreams. To do this, I want to put the fis.close() statement in the finally{} block of my exception handling because the finally block is ALWAYS executed, whether an exception is caught or not (right?). So I do something like this

Code:
import java.io.*;
import java.lang.*;
import java.util.*;

public class Test{
    
    //main function
    public static void main (String args[]) {
           //vars
           String path = null;
           FileInputStream fis = null;
           File file = null;
         try{
             path = new String("V:\\sys\\crss-scrs\\20000\\3000\\800\\60\\veritas\\39788\\qa39788nssf.htm");
             file = new File(path);
             fis = new FileInputStream(file);
             int fileSize = (fis.available());
         }
         catch(IOException e) {System.out.println(e.getMessage());e.printStackTrace();}
         finally { fis.close();}
    }//end main
}//end class

But I get an error message
Code:
Test.java:29: unreported exception java.io.IOException; must be caught or declar
ed to be thrown
                    fis.close();
                             ^
1 error

Which makes sense, but I can't figure out how to make sure I ALWAYS close my file streams (the same thing happens with database connections conn.close()).

Can anybody help? Many thanks!
 
Code:
finally { 
  try {
    if (fis != null) 
          fis.close();
  } catch (IOException ioe) {
    System.err.println("Error closing file.");
    ioe.printStackTrace(System.err);
  }
}


--------------------------------------------------
Free Database Connection Pooling Software
 
That seems redundant though. Suppose fis.close() fails in the try block of the finally block, then the input stream is still open. Or am I just not understanding this right?

Thanks
 
If your fis.close() operation fails then there is nothing you can do ...

So your choices are, leave your fis.close() operation in your original try block, which means you won;t even attempt to close the stream if some code above it fails on a read() operation ow whatever, or do as I have showed in the finally within another try block. There is not any other way of doing it.

--------------------------------------------------
Free Database Connection Pooling Software
 
If your fis.close() operation fails then there is nothing you can do ..."

Good point...

Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top