I\'m writing an app that connect to a website and read one line from it. I do it like this:
try{
URLConnection connection = new URL(\"www.example.com
The general idiom for resource acquisition and release in Java is:
final Resource resource = acquire();
try {
use(resource);
} finally {
resource.release();
}
Note:
try should immediately follow the acquire. This means you can't wrap it in the decorator and maintain safety (and removing spaces or putting things on one line doesn't help:).finally, otherwise it wont be exception safe.null, use final. Otherwise you'll have messy code and potential for NPEs.try block (Java leads you astray here).ou can abstract this nonsense with the Execute Around idiom, so you don't have to repeat yourself (just write a lot of boilerplate).