Skip to content

Instantly share code, notes, and snippets.

@komiya-atsushi
Created February 18, 2014 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komiya-atsushi/9071381 to your computer and use it in GitHub Desktop.
Save komiya-atsushi/9071381 to your computer and use it in GitHub Desktop.
finally で発生する例外を抑制しようと思ったら、こんな実装になってしもた。きもい。
/**
* 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