Java – understand the last value and value returned by try catch

I have the following code

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch value";
    } finally {
        System.out.println("Executing finally");
        return "Return finally value";
    }
}

This output is

Executing try
Executing finally
Return finally value

If I change my finally block without returning anything like it

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch value";
    } finally {
        System.out.println("Executing finally");
    }
}

Then the output is

Executing try
Executing finally
Return try value

Now I see, unless we call system Exit (0), otherwise it will always be executed; Call or JVM crash What I can't understand is why the return value has changed? I still want it to return the value of the try block Anyone can explain why the final value is considered instead of the return value of the try block?

Please do not answer, because even if there is a return in the try block, it will be executed in the end... Or only when there is system Execute only when exit (0); Call or JVM crash as far as I am concerned.

Editor: (based on Dirk's comments)

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return printString("Return try value");
    } catch (Exception e){
        System.out.println("Executing Catch");
        return printString("Return catch value");
    } finally {
        System.out.println("Executing finally");
        return printString("Return finally value");
    }
}

private static String printString(String str){
    System.out.println(str);
    return str;
}

Output:

Executing try
Return try value
Executing finally
Return finally value
Return finally value

Solution

Before returning from the main block, the JVM must ensure that the finally block is executed, so that's what it does The idea is to execute the finally block and then return and execute the return statement from the main block But if you have a return statement in the finally block, it will be executed when the finally block is executed... Which means that control will never return to the main block to complete the return statement

>The JVM encountered a return statement in the main block It pauses the execution of the main block and checks the finally clause. > It completely executes the finally clause, including its return statement. > It will never complete the try block

Note, however, that the return expression of the try block will be evaluated and discarded It's important if it has side effects Therefore, if your main block has returned I, it will have no effect on the return value, but I will still increase it (thanks Dirk for pointing this out.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>