多次返回:哪一个设置最终返回值? [英] Multiple returns: Which one sets the final return value?

查看:123
本文介绍了多次返回:哪一个设置最终返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此代码:

String test() {
    try {
        return "1";
    } finally {
        return "2";
    }
}

语言规范是否定义了呼叫的返回值到 test()?换句话说:每个JVM中它总是一样的吗?

Do the language specifications define the return value of a call to test()? In other words: Is it always the same in every JVM?

在Sun JVM中,返回值是 2 ,但我想确定,这不是VM -dependant。

In the Sun JVM the return value is 2, but I want to be sure, that this is not VM-dependant.

推荐答案

是的,语言规范定义2是结果。如果VM以不同方式执行,则不符合规范。

Yes, the language spec defines that "2" is the result. If a VM does it differently, it's not spec-compliant.

大多数编译器都会抱怨它。例如,Eclipse将声称返回块永远不会被执行,但它是错误的。

Most compilers will complain about it. Eclipse, for example, will claim that the return block will never be executed, but it's wrong.

编写类似的代码这是一个令人震惊的糟糕做法,不要做它:)

It's shockingly bad practice to write code like that, don't ever do it :)

这篇关于多次返回:哪一个设置最终返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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