java - Why does try..finally block not register the original exception as suppressed? -
with following code:
try { throw new runtimeexception ("main"); } { throw new runtimeexception ("finally"); } i result:
exception in thread "main" java.lang.runtimeexception: @ test.main(test.java:12) however, addition of suppressed exceptions in java 7, wouldn't logical language register original "main" exception suppressed when finally block fails exception? have manually emulate this:
try { throw new runtimeexception ("main"); } catch (runtimeexception exception) { try { throw new runtimeexception ("finally"); } catch (runtimeexception exception2) { exception2.addsuppressed (exception); throw exception2; } } to receive more useful (for understanding what's going on) result:
exception in thread "main" java.lang.runtimeexception: @ test.main(test.java:13) suppressed: java.lang.runtimeexception: main @ test.main(test.java:9) edit: clarify i'm wondering. current java version 8, suppressed exceptions not brand new feature. try..finally still doesn't incorporate them. there prevents happening?
because try-with-resources syntactic sugar , java compiler doesn't expand regular try-finally blocks in same way.
take @ following code:
try(fileinputstream fstream = new fileinputstream("test")) { fstream.read(); } when compiled , decompiled (using intellij idea) looks this:
fileinputstream fstream = new fileinputstream("test"); throwable var2 = null; try { fstream.read(); } catch (throwable var19) { var2 = var19; throw var19; } { if(fstream != null) { if(var2 != null) { try { fstream.close(); } catch (throwable var17) { var2.addsuppressed(var17); } } else { fstream.close(); } } } whereas code:
fileinputstream fstream = new fileinputstream("test"); try { fstream.read(); } { fstream.close(); } looks same when compiled , decompiled.
now, case made finally blocks should expanded in same way done above, reason has either been overlooked or decided against.
i suggest open feature request because think it's sensible feature.
Comments
Post a Comment