how does finally executes?

D

dk

suppose I have code anippet

catch(myException) {
//some code
return System.out.println("hi 1 ");
}finally{
System.out.println("hi 2 ");
}

and once the execution reaches the catch block then what would be the
order of print statements
would it be hi2 hi1 and if so then why?
 
C

concerto

catch gets executed when an exception is thrown.

finally get executed no matter what.

So, regardless of if catch is executed, finally will always occur.
 
A

amit

suppose I have code anippet

catch(myException) {
         //some code
         return System.out.println("hi 1 ");}finally{

           System.out.println("hi 2 ");

}

and once the execution reaches the catch block then what would be the
order of print statements
would it be hi2 hi1 and if so then why?

it will print hi 1 then hi 2 ....
 
A

Arne Vajhøj

dk said:
suppose I have code anippet

catch(myException) {
//some code
return System.out.println("hi 1 ");
}finally{
System.out.println("hi 2 ");
}

and once the execution reaches the catch block then what would be the
order of print statements
would it be hi2 hi1 and if so then why?

First catch then finally.

Surprisingly finally is the final thing to happen.

:)

Arne
 
R

Roedy Green

and once the execution reaches the catch block then what would be the
order of print statements
would it be hi2 hi1 and if so then why?

You may have watched too many Star Trek episodes. Throwing an
exception will not cause your computer to melt or explode.

You can answer such questions most quickly with a little experiment
program. Once you think you understand, re-read the docs. They will
then make much more sense.
 
D

dk

First catch then finally.

Surprisingly finally is the final thing to happen.

:)

Arne
thanks Arne for your reply!! I got all I wanted the answer may be some
fun to :)
 
D

dk

Arne
thanks Arne for your reply!! I got all I wanted the answer may be some
fun to :)

well for those who wanted the entire code here it is:


class TestFinally{
public static void main(String a[]){
System.out.println(testFinally());
}

public static String testFinally(){
try{
throw new Exception();
}catch(Exception e){
return "hi 1";
}finally{
System.out.println("hi 2");
}
}
}

so now what would be the explanation of output as
hi 2
hi 1
pls elaborate although finally is the final thing that must happen as
I expect it to be :)
 
L

Lew

dk said:
Arne
thanks Arne for your reply!! I got all I wanted the answer may be some
fun to :)

well for those who wanted the entire code here it is:


class TestFinally{
public static void main(String a[]){
System.out.println(testFinally());
}

public static String testFinally(){
try{
throw new Exception();
}catch(Exception e){
return "hi 1";
}finally{
System.out.println("hi 2");
}
}
}

so now what would be the explanation of output as
hi 2
hi 1
pls elaborate although finally is the final thing that must happen as
I expect it to be :)

The 'finally' is last only inside the 'testFinally()' method. It happens
after determination of the return value, on the way out of the method. The
System.out.println() of "hi 1" occurs in 'main()', after the 'testFinally()'
method is completely finished and therefore after any internal 'finally' of
that method.
 
S

Sarkar

pls elaborate although finally is the final thing that must happen as
I expect it to be :)

If I am right your question is what will be the reasoning behind
output.
hi 2
hi 1

Answer to that question I believe return statement will be executed
after the execution of finally block execution. But in case return
statement is consist of any expiration then it'll be evaluated first
then finally block will executed. As mentioned below example.
public class TestFinally{
public static String testFinally(){
try{
throw new Exception();
}catch(Exception e){
return "Hi 1" +""+method1();
}finally{
System.out.println("Hi 2");
}
}
public static String method1(){
System.out.println("Hi from method1");
return "Kumar";
}
}

out:
Hi from method1()
Hi 2
Hi 1 Hi from method1()

regards, Amit J.
 
A

Arne Vajhøj

dk said:
Arne
thanks Arne for your reply!! I got all I wanted the answer may be some
fun to :)

well for those who wanted the entire code here it is:


class TestFinally{
public static void main(String a[]){
System.out.println(testFinally());
}

public static String testFinally(){
try{
throw new Exception();
}catch(Exception e){
return "hi 1";
}finally{
System.out.println("hi 2");
}
}
}

so now what would be the explanation of output as
hi 2
hi 1
pls elaborate although finally is the final thing that must happen as
I expect it to be :)

First you execute throw new Exception(), it throws an exception so you
jump to the catch, then you execute return "hi 1" and finally
System.out.println("hi 2") gets executed.

And then main continues and execute System.out.println on the previously
returned value.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top