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:
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.
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:
The constructor is ok without exception, and the body is ok too,here is the result:
if we throw exception in the body,call doSomething2 like this:
we got this:
As you can see , the exception is caught, but it’s still closed.
If we throw exception in the constructor , like this:
we got this:
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: