Java try-finally return design question -


in java, try { ... } { ... } executed unintuitively me. illustrated in question, does execute in java?, if have return statement in try block, ignored if block defined. example, function

boolean test () {     try {         return true;     }     {         return false;     } } 

will return false. question: why this? there particular philosophy behind design decision made java? appreciate insight, thank you.

edit: i'm particularly interested 'why' java thinks it's ok violate semantics define. if 'return' in try block, method should return right , there. jvm decides ignore instruction , return subroutine hasn't yet been reached.

technically speaking, return in try block won't ignored if finally block defined, if block includes return.

it's dubious design decision probably mistake in retrospect (much references being nullable/mutable default, and, according some, checked exceptions). in many ways behaviour consistent colloquial understanding of finally means - "no matter happens beforehand in try block, run code." hence if return true finally block, overall effect must to return true, no?

in general, seldom idiom, , should use finally blocks liberally cleaning up/closing resources if ever return value them.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -