3

For example, I have method for working with input/output streams:

    public void doSomethingWithStreams () throws FileNotFoundException, IOException
            {
             OutputStream out1, out2;            
             InputStream in1, in2;
try{                     
            //do something with Streams: read, write, process, etc.
            }
finally{
        //There I try to close connections
        out1.close();
        out2.close();
        in1.close();
        in2.close();
        }    
            }

Method can throws IOException and it is valid behavior. But If I have Exception in this line:

 out1.close();

others three Stream will be NOT closed. What solution can you recommend? How? How close all?

I have just one:

    public void doSomethingWithStreams () throws FileNotFoundException, IOException
            {
             OutputStream out1, out2;            
             InputStream in1, in2;
         try{            
            //do something with Streams: read, write, process, etc.
            }
finally{
        //There I try to close connections

try{out1.close();}
  finally{
     try{out2.close();}
         finally{
            try{in1.close();}
                finally{
        in2.close();}
}}

}

            }

As you can see - my approach is using multiple try-finally blocks.

Do you think it is good idea?

user471011
  • 7,104
  • 17
  • 69
  • 97
  • This could be of your interest: http://stackoverflow.com/questions/7224658/java-try-finally-block-to-close-stream – PeterMmm Sep 11 '12 at 15:18
  • Please can you be a good citizen and learn how to format your code and then fix the formatting in this question. I almost closed this question due to how hard it is to read. Please see: http://stackoverflow.com/editing-help#code Thanks. – Kev Sep 12 '12 at 23:44

3 Answers3

6

If three streams are not dependent on each other, may be having try/catch for each stream look cleaner.

Something like:

try{
 out1.close();
}catch(Exception e)
{
....
}finally

{.... }

try{
        out2.close();
}catch(Exception e)
{
.....
}finally

{.... }

EDIT: As iccthedral suggested, if you use Java7 you may use try-with-resource block.

kosa
  • 65,990
  • 13
  • 130
  • 167
2

Probably the best way to go about it is:

try (
     OutputStream out1 = ...;
     OutputStream out2 = ...;
     InputStream in1 = ...;
     InputStream in2 = ...;
) {
     ...
}
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
2

Perhaps the best way to clean this up is to make a method like this:

public static void close(Closeable c) {
   if (c == null) return; 
   try {
       c.close();
   } catch (IOException e) {
       // Do anything or nothing
   }
}

This can replace your .close() calls and won't throw exceptions if they fail.

Chris Bode
  • 1,265
  • 7
  • 16
  • 2
    This is a good solution. – nullpotent Sep 11 '12 at 15:20
  • @Chris, solution with catch(IOException ex){//Do nothing} is Wrong Solution! It is my opinion. – user471011 Sep 11 '12 at 15:28
  • 2
    @user471011 Yeah, you probably shouldn't be ignoring exceptions. In a serious project you should at the very least be logging them. That's why I left the `Do anything OR nothing` comment there; its up to the reader to decide what needs to be done when a stream fails to close. – Chris Bode Sep 11 '12 at 15:34