Java try-finally return design question

后端 未结 7 2185
滥情空心
滥情空心 2020-11-27 04:04

In Java, a try { ... } finally { ... } is executed somewhat unintuitively to me. As illustrated in another question, Does finally always execute in Java?, if you have a retu

相关标签:
7条回答
  • 2020-11-27 04:16

    Though The finally block is made for closing resources, and a return statement is generally not supposed to be there, and Eclipse warns that "finally block does not complete normally", I found in some circumstances a "finally return" is still desirable.

    ResponseType response = new ResponseType();
    try{
        //set some properties of the response object.
        response.setStatus(1);
        //return response;
    }catch (Exception e){
        //Some other properties of the response object according to the exception.
        response.setStatus(0);
        //return response;
    }finally{
        return response;
    }
    

    If I don't put the return clause in the finally block, I would have to repeat it in try and catch blocks and the current code is a little clearer.

    0 讨论(0)
  • 2020-11-27 04:25

    If code in finally block ends abruptly, it changes return value/exception from try block. This is considered to be bad practice, and you should not do that.

    Among other places, this is also discussed in Java Puzzlers book.

    0 讨论(0)
  • 2020-11-27 04:29

    Purpose of finally:

    • Java people has created finally block for closing operation,which has to be performed in both cases (e.g exception occurred or no exception).

    • It is not recommended to use return statement in finally block as it overrides both the return statement from catch & try block. and warns "finally block does not complete normally"

    • finally block should have return statement only when both try and catch has no return statement.

    • Incase both try and catch have different value then we should not keep anything in finally block,

    • and if we are returning same value in both try and catch block then its better to keep return in finally and remove return in both try & catch block.
    • If finally throws any exception then the return inside that block will not execute.

    finally block will not execute only when:

    • On invoke of System.exit()
    • kill process
    • application/environment crashes
    • stackoverflow/infinite loop
    0 讨论(0)
  • 2020-11-27 04:33

    If you call javac with -Xlint, then an appropriate warning will be generated indicating that you should not call return from a finally clause. For example (compiling a simple class with the above test() method):

    javac -Xlint foo.java
    foo.java:13: warning: [finally] finally clause cannot complete normally
        }
    ^
    1 warning
    
    0 讨论(0)
  • 2020-11-27 04:34

    Technically speaking, the return in the try block won't be ignored if a finally block is defined, only if that finally block also includes a return.

    It's a dubious design decision that was probably a mistake in retrospect (much like references being nullable/mutable by default, and, according to some, checked exceptions). In many ways this behaviour is exactly consistent with the colloquial understanding of what finally means - "no matter what happens beforehand in the try block, always run this code." Hence if you return true from a finally block, the overall effect must always to be to return true, no?

    In general, this is seldom a good idiom, and you should use finally blocks liberally for cleaning up/closing resources but rarely if ever return a value from them.

    0 讨论(0)
  • 2020-11-27 04:34

    check this for reference  Does finally always execute in Java?

    "A try statement with a finally block is executed by first executing the try block. Then there is a choice:
    If execution of the try block completes normally, [...]
    If execution of the try block completes abruptly because of a throw of a value V, [...]
    If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:
    If the finally block completes normally, then the try statement completes abruptly for reason R.
    If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded)."

    0 讨论(0)
提交回复
热议问题