Dominic: That is -extremely- bad and buggy code. NEVER use that, ever. The problem is this:
If the try block does NOT throw an exception, but the close method DOES, then you should not swallow that exception like closeDontThrow() supposedly does. Remember, some outputstreams buffer; odds are that no actual I/O takes place until you call the close() method! It's acceptable to swallow *INPUTSTREAM*'s close method's IOException, not so much because you can safely ignore that exception, but more because it never gets thrown anyway, so its a moot point. Rethrowing as a RuntimeException (throw new RuntimeException(e)) is a wiser plan, though. The only 'right' way to do it, without ARM or closures, is: try { boolean swallowClose = false; InputStream stream = getStream(); try { //do stuff here } catch ( IOException e ) { swallowClose = true; throw e; } finally { try { stream.close(); } catch ( IOException e ) { if ( !swallowClose ) throw e; } } } catch ( IOException e ) { //THIS is where you should handle I/O issues! } a handful, isn't it? And can you believe some people are against the ARM proposal? I say those people clearly hate life and adore boilerplate. with arm, that would become: try ( InputStream stream = getStream() ) { //do stuff here } catch ( IOException e ) { //Handle Exceptions here } On Jun 3, 8:30 am, Dominic Mitchell <d...@happygiraffe.net> wrote: > On 3 Jun 2009, at 03:55, Christian Catchpole wrote: > > > > > Yeah, this can be a problem. Perhaps the neatest way is to do > > something like this.. use a static helper which eats the exception. > > > OutputStream os = new BlahOutputStream(); > > try { > > // do stuff hyar, hyar and hyar.. > > os.write( stuff ); > > } finally { > > StreamHelperThingy.closeDontThrow(os); > > } > > Most code I've seen uses commons-io's IOUtils.closeQuietly() to do this… > > http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html > > -Dom --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javaposse@googlegroups.com To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---