Created
February 18, 2014 13:53
-
-
Save komiya-atsushi/9071381 to your computer and use it in GitHub Desktop.
finally で発生する例外を抑制しようと思ったら、こんな実装になってしもた。きもい。
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
/** | |
* finally で発生する例外を抑制しようと思ったら、こんな実装になってしもた。 | |
*/ | |
public class HowToSupressExceptions { | |
public static void main(String[] args) throws Exception { | |
new Suppressible() { | |
@Override | |
public void doTry() throws Exception { | |
throw new Exception("本体処理にてなんかの例外が発生した。"); | |
} | |
@Override | |
public void doFinally() throws Exception { | |
throw new Exception("こちらは抑制される。"); | |
} | |
}.run(); | |
} | |
public static abstract class Suppressible { | |
public abstract void doTry() throws Exception; | |
public abstract void doFinally() throws Exception; | |
public void run() throws Exception { | |
try (AutoCloseable ac = new AutoCloseable() { | |
@Override | |
public void close() throws Exception { | |
doFinally(); | |
} | |
}) { | |
doTry(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment