Java 我们可以在 finally 块中使用“返回”吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18205493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:18:07  来源:igfitidea点击:

Can we use "return" in finally block

javaexceptiontry-catch-finallyfinally

提问by Rakesh KR

Can we use return statement in finallyblock. Can this cause any problem?

我们可以在finally块中使用 return 语句吗?这会导致任何问题吗?

采纳答案by Ankur Lathi

Returning from inside a finallyblock will cause exceptionsto be lost.

finally块内部返回将导致exceptions丢失。

A return statement inside a finally block will cause any exception that might be thrown in the try or catch block to be discarded.

finally 块中的 return 语句将导致任何可能在 try 或 catch 块中抛出的异常被丢弃。

According to the Java Language Specification:

根据Java 语言规范:

If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and 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).

如果 try 块的执行由于任何其他原因 R 突然完成,则执行 finally 块,然后有一个选择:

   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).

Note: As per JLS 14.17- a return statement always completes abruptly.

注意:根据JLS 14.17- return 语句总是突然完成。

回答by Krushna

Yes you can write the return statement in a finally block and it will override the other return value.

是的,您可以在 finally 块中编写 return 语句,它将覆盖其他返回值。

EDIT:
For example in below code

编辑:
例如在下面的代码

public class Test {

    public static int test(int i) {
        try {
            if (i == 0)
                throw new Exception();
            return 0;
        } catch (Exception e) {
            return 1;
        } finally {
            return 2;
        }
    }

    public static void main(String[] args) {
        System.out.println(test(0));
        System.out.println(test(1));
    }
}

The output is always 2, as we are returning 2 from the finally block. Remember the finally always executes whether there is a exception or not. So when the finally block runs it will override the return value of others. Writing return statements in finally block is not required, in fact you should not write it.

输出总是 2,因为我们从 finally 块中返回 2。请记住,无论是否有异常,finally 都会执行。所以当 finally 块运行时,它将覆盖其他块的返回值。在 finally 块中编写 return 语句不是必需的,实际上您不应该编写它。

回答by Prasad Kharkar

You can write returnstatement in finallyblock but the value returned from tryblock will be updated on the stack and not the finallyblock return value.

您可以returnfinally块中编写语句,但是从try块返回的值将在堆栈上更新,而不是finally块返回值。

Let us say you have this function

让我们说你有这个功能

private Integer getnumber(){
Integer i = null;
try{
   i = new Integer(5);
   return i;
}catch(Exception e){return 0;}
finally{
  i = new Integer(7);
  System.out.println(i);
}
}

and you are calling this from main method

你是从主方法调用这个

public static void main(String[] args){
   System.out.println(getNumber());
}

This prints

这打印

7
5

回答by Suresh Atta

Yes you can,But you should not 1 ,because the finally blockis meant for a special purpose.

是的,你可以,但你不应该 1 ,因为 finally 块是为了特殊目的。

finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

finally 不仅仅用于异常处理——它允许程序员避免通过 return、continue 或 break 意外绕过清理代码。将清理代码放在 finally 块中始终是一个好习惯,即使没有预料到异常也是如此。

Writing your logic inside it is not recommended.

不建议在其中编写逻辑。