try-with-resource
block.
try (OpenedDocument document = ((Renderer) renderer).openDocument("DSN", new TmpCopyFromString("w2mlstring", W2MLDocumentType.class))) { // document will automatically be closed, when we leave the try // block - no matter, if regularly or caused by exception. } catch (RendererException e) { // Exceptions during opening or rendering } catch (Exception e) { // Exception during close }<
If try-with-resource is not applicable in your code, the close method can also be invoked directly. The following code is exactly equivalent to the solution above:
OpenedDocument document = null; try { document = ((Renderer) renderer).openDocument("DSN", ew TmpCopyFromString("w2mlString", W2MLDocumentType.class)); } catch (RendererException e) { // Exceptions during opening or rendering // ... } finally { try { if (document != null) document.close(); } catch (Exception e) { // Exception during close } }