我需要解释下面的代码 .
public class ReturnValueFromTryCatchFinally
{
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";
}
}
}
我的理解是最终块将始终执行,因此s的值将是“从最后块的try块返回值返回值” . 但是上面的代码给出了try块的s值 . 即;从try块返回值 . 为什么s的值从try块返回 . 据我所知,最后块将始终执行 . 那么s的 Value 不会改变吗?在try,catch块中有关于变量范围的任何我需要理解的东西 .
这不仅发生在字符串上 . 即使我从try块返回整数,它也会发生 .