Monday, 26 August 2013

java 7 - Try/finally how does it know what method to call to cleanup resources in 3rd party api

java 7 - Try/finally how does it know what method to call to cleanup
resources in 3rd party api

In java 7, instead of
try {
fos = new FileOutputStream("movies.txt");
dos = new DataOutputStream(fos);
dos.writeUTF("Java 7 Block Buster");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
dos.close();
} catch (IOException e) {
// log the exception
}
}
You can do this
try (FileOutputStream fos = new FileOutputStream("movies.txt");
DataOutputStream dos = new DataOutputStream(fos)) {
dos.writeUTF("Java 7 Block Buster");
} catch (IOException e) {
// log the exception
}
But I am working with a 3rd party API, this api requires that .close() be
called to cleanup the open resource
try (org.pdfclown.files.File file = new
org.pdfclown.files.File("movies.pdf")) {
...
}
How will java 7 know how to handle a 3rd party API like this? How will it
know what method to call?

No comments:

Post a Comment