On Thu, Jun 04, 2009 at 05:25:34AM -0700, Reinier Zwitserloot wrote: > Dominic: That is -extremely- bad and buggy code. NEVER use that, ever.
Tell that to the Cocoon guys... That's where I encountered it. > 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 > } I really, really want project COIN. Making it part of Java 5 would have been nice. :) Serisouly, this sort of thing is exactly what pushes me towards scala / groovy / jruby. The boilerplate involved in something as simple as writing a file _correctly_ is phenomenal. FWIW I've been dealing with similar boilerplate issues in Perl. For all it's status as a good "glue" language, it's annoyingly difficult to work out what went wrong in a call to system()... -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 -~----------~----~----~----~------~----~------~--~---