Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:18:37+00:00 2026-06-11T03:18:37+00:00

For example, I have method for working with input/output streams: public void doSomethingWithStreams ()

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:18:39+00:00Added an answer on June 11, 2026 at 3:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have a method -myFooBarMethod:withFoo:bar:moreFoo: and inside the implementation of that method I
For example I have a method that looks like this [self performSelectorOnMainThread:@selector(someMethod) withObject:data waitUntilDone:YES];
Example: I have a selector like this, which I give to another method as
I wants to access a method in different project. as example i have called
If I have for example a class with instance method and variables class Foo
I have a question regarding providing a custom delete-method to boost::shared_ptr constructor. For example,
for example we have BaseClass myBaseObject InheritedClass myInheritedObject and 2 overloaded methods void Do(BaseClass
I have found following working example of CGI with bash. If I change first
I installed the JQuery token-input plugin. https://github.com/loopj/jquery-tokeninput Autocomplete is working, but I have a
I am working with Android's standard softkeyboard input method editor. I am showing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.