trying out the prototype

David Holmes David.Holmes at oracle.com
Tue Aug 24 04:14:10 PDT 2010


Gernot Neppert said the following on 08/24/10 19:27:
> 2. If you use multiple Resources, an exception thrown by one of them
> will suppress exceptions thrown by the 'close()' invocation of others.
> While I can see some sense in suppressing exceptions from 'close()' of
> the same instance, I cannot see why this reasoning should apply to
> other instances.
> (I guess this forms a case against the try-with-multiple-resources
> statement in general. The list of semicolon-delimited declarations
> enclosed by parentheses looks weird, anyway ;-)

I tend to agree the syntax is awkward and far less readable than simply 
nesting the try-with statements.

David Holmes

> Here's an example:
> 
> class ThrowsOnRead extends Reader
> {
>     public int read(char[] cbuf, int off, int len) throws IOException
>    {
>       throw new IOException();
>    }
> 
> 	@Override
> 	public void close() throws IOException
>        {
> 		
>        }
> }
> 
> class ThrowsOnClose extends Reader
> {
>     public void close() throws IOException
>    {
>      throw new IOException();
>    }
> 
>     public int read(char[] cbuf, int off, int len) throws IOException
>    {
>      return 0;
>    }
> }
> 
> 
> 
> public class TestAutoClose {
> 
> 	public static void main(String[] args) throws IOException {
> 		try(Reader rdr1 = new ThrowsOnRead(); Reader rdr2 = new ThrowsOnClose())
> 		{
> 		    rdr1.read();  // Why is this exception more important than the
> one thrown by rdr2.close() ?
> 		}
> 	}
> }
> 



More information about the coin-dev mailing list