Skip to content

Instantly share code, notes, and snippets.

@gaul
Last active December 22, 2015 07:59
Show Gist options
  • Save gaul/6442289 to your computer and use it in GitHub Desktop.
Save gaul/6442289 to your computer and use it in GitHub Desktop.
Test Closer behavior with Java 6 and 7.
import java.io.Closeable;
import java.io.IOException;
import com.google.common.io.Closer;
public class CloserTest {
private static void testCloserJava6() throws Exception {
Closer closer = Closer.create();
try {
Closeable closeable = closer.register(
new ThrowIOExceptionOnClose());
throw new Exception("from body");
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
private static void testCloserJava7() throws Exception {
// comment for Java 6
try (Closer closer = Closer.create()) {
closer.register(new ThrowIOExceptionOnClose());
throw new Exception("from body");
}
}
public static void main(String[] args) throws Exception {
if (args.length == 1) {
if (args[0].equals("java6")) {
testCloserJava6();
} else if (args[0].equals("java7")) {
testCloserJava7();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment