Skip to content

Instantly share code, notes, and snippets.

@trasa
Created November 1, 2018 20:58
Show Gist options
  • Save trasa/5ca31019507fc9a5426ff91d6dca57e8 to your computer and use it in GitHub Desktop.
Save trasa/5ca31019507fc9a5426ff91d6dca57e8 to your computer and use it in GitHub Desktop.
Java Try-With-Resources Example
@Slf4j
public class TWR implements Closeable() {
public void kaboom() throws IOException {
log.info("kaboom");
throw new IOException("io exception here");
}
@Override
public void close() {
log.warn("CLOSED!!");
}
// test method
public static void testMe() throws IOException {
try(TWR c = new TWR()) {
c.kaboom();
}
}
}
/* output:
2018-11-01 13:52:28 [main] INFO - kaboom
2018-11-01 13:52:28 [main] WARN - CLOSED!!
java.io.IOException: io exception ...
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment