相关疑难解决方法(0)

finally块总是在Java中执行吗?

考虑到这一点的代码,我可以绝对肯定的是,finally块总是执行,不管something()是什么?

try {  
    something();  
    return success;  
}  
catch (Exception e) {   
    return failure;  
}  
finally {  
    System.out.println("I don't know if this will get printed out");
}
Run Code Online (Sandbox Code Playgroud)

java return try-catch-finally

2281
推荐指数
38
解决办法
48万
查看次数

try {} finally {}使用返回值构造

我想知道为什么Java编译器会接受以下代码:

public class Main {

    public static void main(String ... args){
        System.out.println("a() = " + a());
    }

    public static String a (){
        try {
            return "a";
        }catch(Throwable t){
        }finally{
            return "b";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以也不应该奏效.java规范声明finally始终执行块,但同时已指定返回值.所以要么你不能执行该return "b"语句,因为你已经退出return "a",这是不正确的.

但是,另一个选择是执行该return "b"语句,从而完全忽略该return "a"语句......

我会说两者都是错的,我希望这不会编译.然而,它编译并运行良好.我将把答案作为一个很好的练习留给读者;).

基本上我的问题是:除了不好的做法,这会被认为是一个Java错误,还是除了混淆之外还有其他奇妙的用途?

编辑:

问题不是如果它是一个bug,已经得到了回答,但它有不错的用例吗?

java

26
推荐指数
1
解决办法
497
查看次数

奇怪的是最后的行为?

public class Test2 {

    public static void main(String[] args) {
        Test2 obj=new Test2();
        String a=obj.go();

        System.out.print(a);
    }


    public String go() {
        String q="hii";
        try {
            return q;
        }
        finally {
            q="hello";
            System.out.println("finally value of q is "+q);
        }
    }
Run Code Online (Sandbox Code Playgroud)

为什么hii从函数返回后打印go(),在finally块中值已更改为"hello"?

该计划的输出是

finally value of q is hello
hii
Run Code Online (Sandbox Code Playgroud)

java finally

14
推荐指数
1
解决办法
873
查看次数

try/catch/finally的惊人输出?

我运行这段代码:

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

private static int catcher() {
    try {
        System.out.println("TRY");
        thrower();
        return 1;
    } catch (Exception e) {
        System.out.println("CATCH");
        return 2;
    } finally {
        System.out.println("FINALLY");
        return 3;
    }
}

private static void thrower() {
    throw new RuntimeException();
}
Run Code Online (Sandbox Code Playgroud)

我希望在输出中看到这个:

TRY
CATCH
FINALLY
2
Run Code Online (Sandbox Code Playgroud)

但令人惊讶的是输出是:

TRY
CATCH
FINALLY
3
Run Code Online (Sandbox Code Playgroud)

我糊涂了.return 2声明在哪里?这是return at finally一种不好的做法吗?

java try-catch

6
推荐指数
1
解决办法
414
查看次数

在 try 块中返回之后,javascript finally 块的执行顺序是什么

关于:Java try-finally 返回设计问题

这并不能完全回答我的问题,也可能不相关,因为它是 java 而不是 javascript:try、catch 和 finally 中的执行顺序是什么

我知道将 return 放入finally块中将优先于catch块中的 return ,但是序列事件如何与以下代码一起使用?

function confusion(){
  var test = "a";
  try{
    return test;
  }
  finally{
    test = "b";
  }
}

console.log("Result", confusion()); //"a"
Run Code Online (Sandbox Code Playgroud)

我对返回任何东西的一般理解是控制通常返回到调用方法的上下文,因此继续执行的事实让我感到困惑。

在这种情况下,我认为它可能会像这样:

- 函数设置为返回test->finally更改值test->test返回更新后的值。

但相反,它似乎为该try块保留了某种状态并finally错过了时间?

javascript

6
推荐指数
1
解决办法
2898
查看次数

在java中,如果try和catch都抛出相同的异常并最终返回怎么办?

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

    static int method() {
        try {
            throw new Exception();
        }
        catch(Exception e) {
            throw new Exception();
        }
        finally {
            return 4;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么返回值为4?

java exception-handling

4
推荐指数
1
解决办法
5151
查看次数

在catch块中没有抛出异常的异常

在Java中使用异常处理时,我注意到在Java中的catch块中执行某些非法运行时操作时不会抛出异常.

这是语言中的错误还是我错过了什么?有人可以调查一下 - 因为为什么没有从catch块中抛出异常.

public class DivideDemo {

    @SuppressWarnings("finally")

    public static int divide(int a, int b){

    try{
       a = a/b;
    }
    catch(ArithmeticException e){
       System.out.println("Recomputing value");

       /* excepting an exception in the code below*/
       b=0;
       a = a/b;
       System.out.println(a);
    }
    finally{
      System.out.println("hi");
      return a;
    }
  }    
  public static void main(String[] args) {
     System.out.println("Dividing two nos");
     System.out.println(divide(100,0));
  }
Run Code Online (Sandbox Code Playgroud)

}

java exception-handling divide-by-zero

2
推荐指数
1
解决办法
2870
查看次数

如果尝试并最终在方法中返回布尔值,会发生什么情况

我有一个班级-公共班级Check {

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


public boolean isRight(){
    try{
        return true;
    }finally{
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

isRight方法在finally子句中返回true和false,并且由于将肯定调用finally子句,因此最终它返回false,所以try中的数据实际发生了什么。我需要知道输出背后的逻辑原因,但我无法正确理解。

java

2
推荐指数
1
解决办法
981
查看次数