Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try / finally block question

Tags:

java

try-catch

Found this question here

And I can't understand, why on first case it prints CoolReturn+1 and on second case CoolReturn? How does it work?

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 more difficult:

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

like image 494
VextoR Avatar asked Mar 29 '11 18:03

VextoR


People also ask

What is finally block with example?

The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection. The finally block executes whether exception rise or not and whether exception handled or not. A finally contains all the crucial statements regardless of the exception occurs or not.

What is a try finally block?

By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block run when control leaves a try statement.

Can we write TRY block in finally block?

No, we cannot write any statements in between try, catch and finally blocks and these blocks form one unit.

What is exception handling * Your answer?

Exception handling ensures that the program's flow does not break when an exception occurs. For example, suppose a program contains many statements, and an exception occurs in the middle of executing some of them.


4 Answers

Consider

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

as

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, the builder is returned as result. Then line 3 is executed, and the builder gets appended by +1, then the ret is returned which is a "reference" to the object referenced by builder. The same is for the second case. Hope it is clear.

like image 148
khachik Avatar answered Sep 28 '22 13:09

khachik


The first one:

The finally will always fire (assuming the machien doesn't crash or anything). So after the return, the finally block fires, and since it has a reference to the object "builder", it appends the extra token to it.

The second one:

The finally block fires just like before, but it sets the reference to builder to be null. The object still exists, because it still has a link to it.

like image 22
corsiKa Avatar answered Sep 28 '22 11:09

corsiKa


When it does return builder.append("Return") a copy of the reference to the builder variable is pushed onto the stack. Then builder is set to null. The caller then pops the copy of the reference off the stack.

like image 22
TofuBeer Avatar answered Sep 28 '22 13:09

TofuBeer


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

In the second example, in the finally block, you change your pointer to the string builder to be the null pointer, after you return the result from the append method. This does not modify the string builder that you formerly pointed to in any way.

EDIT: Looks like I got beat to it.

like image 28
Barend Venter Avatar answered Sep 28 '22 11:09

Barend Venter