java 7 try-with-resource exception catching

Java 7 has a new try-with-resource grammar, if you haven’t used it and want to know more about it, read more.

The Old way (before java 7) to use the IO objects ,we should write the code like this:

public void testConnection() throws Exception {
	Connection conn = null;
	try {
		conn = ConnectionFactory.getConnection();
	}finally {
		if(conn!=null) {
			conn.close()
		}
	}
}

If the “conn = ConnectionFactory.getConnection();” or the “conn.close()” throws Exception , it would be catched and printed out.

Now using java 7 new try-with-resource grammar, you can define an AutoCloseable class,which would be auto released by the java runtime.

Firstly, we define our “AutoCloseable” object for test.

    static class MyResource implements AutoCloseable {
        private final String name;

        public MyResource(String name) throws Exception {
            this.name = name;
            if(name.equals("testex")) {
            	throw new Exception("ex in cons");
            }
        }
        @Override
        public void close() throws Exception {
            System.out.println(name+" closed");
        }

        public void doSomething() {
            System.out.println(name+" doing");
        }
        public void doSomething2() throws Exception {
            System.out.println(name+" doing");
            throw new Exception("test2");
        }
    }

Here we defined an autocloseable class implements the java interface AutoCloseable, and it has a constructor with a name,when it is closed,it would print it out. Now we use it:

public static void main(String[] args) throws Exception {
	try(MyResource myResource = new MyResource("testr1")) {
		myResource.doSomething();
	}
}

The constructor is ok without exception, and the body is ok too,here is the result:

testr1 doing
testr1 closed

if we throw exception in the body,call doSomething2 like this:

                myResource.doSomething2();//it would throw an exception

we got this:

java.lang.Exception: test2
	at com.ais.brm.test.java7.TestTryWithResource$MyResource.doSomething2(TestTryWithResource.java:38)
	at com.ais.brm.test.java7.TestTryWithResource.main(TestTryWithResource.java:12)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
testr1 doing
testr1 closed

As you can see , the exception is caught, but it’s still closed. If we throw exception in the constructor , like this:

          MyResource myResource = new MyResource("testex")   //would throw an exception   

we got this:

java.lang.Exception: ex in cons
	at com.ais.brm.test.java7.TestTryWithResource$MyResource.<init>(TestTryWithResource.java:25)
	at com.ais.brm.test.java7.TestTryWithResource.main(TestTryWithResource.java:11)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

As you can see, the exception is caught,no object no need to close it.

You can find detail android documents about the data intent-filter here: