Skip to content

Instantly share code, notes, and snippets.

@Lambeaux
Created November 9, 2017 23:09
Show Gist options
  • Save Lambeaux/ae75875f6ac89d9f94830705841dd068 to your computer and use it in GitHub Desktop.
Save Lambeaux/ae75875f6ac89d9f94830705841dd068 to your computer and use it in GitHub Desktop.
Proof that all exceptions, both operations within the try block, as well as the close, will be caught in a single catch and that embedded try blocks are unnecessary.
package com.connexta;
import static com.connexta.Utils.print;
import java.io.Closeable;
import java.io.IOException;
public class TryWithResourcesIgnoresClose {
static void run() {
try (ExampleAutoCloseable ex = new ExampleAutoCloseable()) {
ex.doStuff();
} catch (IOException e) {
print("Catching exception");
}
}
private static class ExampleAutoCloseable implements Closeable {
public void doStuff() throws IOException {
print("Nothing wrong here");
}
@Override
public void close() throws IOException {
throw new IOException("Haha I got through");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment