Created
November 9, 2017 23:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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