Re: Throwing Constructor Exceptions and cleaning up



On Apr 14, 4:02 pm, "Larry K. Wollensham" <lkw4...@xxxxxxxxx> wrote:
Lew wrote:
Another approach puts separate try blocks around things that can fail.

   public MyClass (int arg1) throws IOException
   {
     if (arg1 < 0) throw new IllegalArgumentException();
     InputStream w = new FileInputStream(getFile(arg1));
     assert w != null;
     try
     {
       someSetupStuffThatMayThrow();
     }
     finally
     {
       w.close();
     }
   }

That'll close w no matter what, and doesn't assign it to an instance
variable. If the stream is simply consulted during construction, but
isn't retained open as a part of the object, this makes sense. If it is,
you need the success flag or something similar again so the finally
clause only closes it conditionally, and you need to assign it to
something visible to the rest of the class.

You're right. I should not have gotten rid of the 'success' flag.

What I should have said:

public MyClass (int arg1) throws IOException
{
if (arg1 < 0) throw new IllegalArgumentException();
wrapped = new FileInputStream(getFile(arg1));
assert wrapped != null;
boolean success = false;
try
{
someSetupStuffThatMayThrow();
success = true;
}
finally
{
if ( ! success )
{
wrapped.close();
}
}
}

To get rid of the 'success' variable, have a catch block catch the
exceptions from 'someSetupStuffThatMayThrow()' and close the stream.

public MyClass (int arg1) throws IOException
{
if (arg1 < 0) throw new IllegalArgumentException();
wrapped = new FileInputStream(getFile(arg1));
assert wrapped != null;
try
{
someSetupStuffThatMayThrow();
}
catch ( SetupException exc )
{
final String msg = "setup failed";
logger.error( msg );
try
{
wrapped.close();
}
catch ( IOException ioex )
{
logger.error( "wrapped.close() failed" );
}
throw new IllegalStateException( msg, exc );
}
}

--
Lew
.



Relevant Pages

  • Re: Throwing Constructor Exceptions and cleaning up
    ... Remember that finally executes on any exit from the try block, including success. ... public MyClass throws IOException { ... The finally block cleans up by closing the InputStream only if a success flag wasn't put up at the end of the try body. ...
    (comp.lang.java.programmer)
  • Re: feof(), fseek(), fread()
    ... An input stream can be connected to the user's ... with varying degrees of success. ... fgetc() could have used a trick to report "end of file" ... Now, about that fourth item, the "EOF character" ... ...
    (comp.lang.c)
  • Re: Throwing Constructor Exceptions and cleaning up
    ... and not a destructor, everything is GC'ed at process exit, ... Remember that finally executes on any exit from the try block, including success. ... private final InputStream wrapped; ... public MyClass (int arg1) throws IOException { ...
    (comp.lang.java.programmer)
  • recordeing Encoder Stream to Disk
    ... stream, broadcast by Windows Media Encoder, and save it to disk. ... The app can open the stream but cannot save it, ... bool WMStreamReader::Init ... bool Success = false; ...
    (microsoft.public.windowsmedia.sdk)
  • Sound from PC to Pocket PC
    ... pretty close to success. ... PC's sounds to my Tilt. ... the stream with my Tilt, not in the WMP I get a bad parameter error. ... successfully brought "What you hear" sounds from your home computer to ...
    (microsoft.public.pocketpc)