try catch中的返回值最终在java中阻塞 [英] Return value in try catch finally blocks in java

查看:142
本文介绍了try catch中的返回值最终在java中阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解释下面的代码。



I need explanation for the below code.

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

    static String methodReturningValue()
    {
        String s = null;
        try
        {
            s = "return value from try block";
            return s;
        }
        catch (Exception e)
        {
            s = s + "return value from catch block";
            return s;
        }
        finally
        {
            s = s + "return value from finally block";
        }
    }
}
public class ReturnValueFromTryCatchFinally
{





我的尝试:



我的理解是finally块将始终执行,因此s的值将是从finally块返回值返回值。但是上面的代码给出了try块的s值。即;从try块返回值。为什么s的值从try块返回。据我所知,最后块将始终执行。那么s的价值不会改变吗?有什么我需要理解的内容在try,catch块中的变量的范围。



What I have tried:

My understanding is that finally block will be executed always and so that the value of s will be "return value from try block return value from finally block". But the above code gives me the value of s from try block. i.e; return value from try block. Why is that the value of s is returning from try block . As far as I know Finally block will be always executed. So won't the value of s be changed? Is there anything that I need to understand on Scope of the variable inside try, catch block.

推荐答案

字符串是不可变的。一旦创建,它们就无法更改。在你的s = s +语句中,你要分配一个带有操作结果的新字符串。



在Try块中,你分配了一个字符串, 从try块返回值。然后使用return语句将指针推送到堆栈上的字符串。



现在,在Finally块中,将两个字符串连接在一起然后不要任何结果字符串。请记住,指向原始字符串的指针是被压入堆栈以返回调用者的指针,而不是s值或指向新字符串的指针。



现在,如果你尝试在Finally块中放一个return语句,你会发现你不能。



这样做的正确方法是不要将返回值放在Try和Catch块中,而是完全放在try / catch / finally块之后。

Strings are immutable. Once created, they cannot be changed. In your "s = s +" statements, you're allocating a new string with the result of the operation.

In the Try block, you allocated a string, "return value from try block". You then pushed the pointer to that string on the stack using the return statement.

Now, in your Finally block, you concatenate two strings together and then don't do anything with the resulting string. Remember, the pointer to the original string is what was pushed onto the stack for return to the caller, not a "value of s" or the pointer to the new string.

Now, if you try putting a return statement in the Finally block, you'll find that you can't.

The proper way to do this would be to NOT put the returns in the Try and Catch blocks, but after the try/catch/finally block entirely.
public static void main(String[] args)
{
    System.out.println(methodReturningValue());
}

static String methodReturningValue()
{
    string s = null;

    try
    {
        s = "return value from try block";
    }
    catch (Exception e)
    {
        s = s + "return value from catch block";
    }
    finally
    {
        s = s + "return value from finally block";
    }

    return s;
}



是的,我是那些订阅每个方法一个返回语句范例的人之一。


Yes, I'm one of those people who subscribes to the "one return statement per method" paradigm.


这篇关于try catch中的返回值最终在java中阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆