Try / finally block question

Found this question here

And I can’t understand why in the first case it prints CoolReturn+1 and in the second case CoolReturn ? How it works?

thanks

=====================

What will be printed?

 public void testFinally(){ System.out.println(setOne().toString()); } protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); return builder.append("Return"); }finally{ builder.append("+1"); } } 

Answer: CoolReturn + 1

A bit trickier:

 public void testFinally(){ System.out.println(setOne().toString()); } protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); return builder.append("Return"); }finally{ builder=null; /* ;) */ } } 

Answer: CoolReturn

+7
source share
7 answers

Consider

 protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); return builder.append("Return"); }finally{ builder.append("+1"); } } 

but

 protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); StringBuilder ret = builder.append("Return"); // 1 return ret; // 2 }finally{ builder.append("+1"); //3 } } 

line 1 is executed, builder returned as the result. Then line 3 is executed, and builder added +1 , then ret returned, which is a "link" to the object referenced by builder . The same goes for the second case. Hope this is understandable.

+7
source

First:

Finally, it will always fire (assuming the machien is not crashing or something else). Thus, after the return, the block finally lights up, and since it has a link to the "builder" object, it adds an additional token to it.

Second:

The finally block starts as before, but sets the link to the builder as null. The object still exists because it still has a reference to it.

+6
source

When it returns builder.append ("Return"), a copy of the link to the builder variable is pushed onto the stack. Then the builder is null. The caller then pops a copy of the link from the stack.

+2
source

In the first example, in the finally block, you manipulate the row builder with the append.

In the second example, in the finally block, you change the pointer to the row builder as a null pointer after you return the result from the add method. This does not change the line builder that you previously pointed to.

EDITOR: Looks like I beat him.

+1
source

The finally statement always starts, so the builder returns, and then +1 is returned.

In the second, the builder is null, so it has nothing to add. It would also be easy to build builder = "" in the latter.

0
source

The finally block will always execute. http://download.oracle.com/javase/tutorial/essential/exceptions/finally.html That's why all three statements are added.

0
source

This works because the return expression is evaluated before the finally block is executed. This is pretty obvious when you call a method in the return your code and add registration instructions to your try and finally blocks. A corresponding explanation can be found in the JLS 3rd edition, 14.20.2 . This is one of the reasons why return in the finally block raise a warning in the IDE, such as Eclipse.

Groovy code example:

 def doSomething() { def f = "something"; try { return f += doSomethingMore() } finally { println "before nulling"; f = null; println "after nulling"; } } def doSomethingMore() { println "doSomethingMore called" return "-wow"; } println "output from call ---> " + doSomething() 
0
source

All Articles