Hardik Patel
Hardik Patel

Reputation: 937

Finally block does not set values of variable in java

I have the below code.

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try{
            System.out.println("Hardik::"+testFinnalyBlock());  
        }catch(Exception e){
            System.out.println("hhh");
        }
    }


    public static int testFinnalyBlock() throws Exception{
        int a=5,b=10;
        int sum=0;
        try{
            sum = a+b;
            System.out.println("sum==="+sum);
            return sum;
        }catch(Exception e){
            System.out.println("In Catch");
        }finally{
            sum = a+30;
            System.out.println("sum==="+sum);
//          return 1;
        }
        return 1;
    }

The output of above code it Hardik::15, While i think it should be Hardik::35.

Can Anyone tell me how it works. Thanks.

Upvotes: 3

Views: 2038

Answers (5)

Simmant
Simmant

Reputation: 1513

remove comment from // return 1; and put return sum; there you get your answer .

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try{
        System.out.println("Hardik::"+testFinnalyBlock());  
    }catch(Exception e){
        System.out.println("hhh");
    }
}


public static int testFinnalyBlock() throws Exception{
    int a=5,b=10;
    int sum=0;
    try{
        sum = a+b;
        System.out.println("sum==="+sum);
        return sum;
    }catch(Exception e){
        System.out.println("In Catch");
    }finally{
        sum = a+30;
        System.out.println("sum==="+sum);
 return sum;
    }

}

Upvotes: 0

Shashi
Shashi

Reputation: 1235

you are using return statement under the try block and sum=a+b and print statement does not throw any exception that is why control goes back to main method with the value of sum 15 that is why your Output is Hardik::15. if you want the output as expected you should remove return statement from try and where you put the line return 1, replace it with return sum
like :

 public static int testFinnalyBlock() throws Exception{
    int a=5,b=10;
    int sum=0;
    try{
        sum = a+b;
        System.out.println("sum==="+sum);
    }

   catch(Exception e){
        System.out.println("In Catch");
        return 1; 
   }finally{
        sum = a+30;  
    }
    return sum;
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

The finally block is being executed, based on your output...

sum===15
sum===35
Hardik::15

The problem is, the return statement in the try-catch section. finally won't update the value begin returned to the caller, because that value has already being placed in another part of memory...

Update

I'm a pretty old school, so I believe in one entry point and one exit point for all my methods...

Something like the following would produce the result you're trying to get...

public static int testFinnalyBlock() throws Exception {
    int a = 5, b = 10;
    int sum = 0;
    try {
        sum = a + b;
        System.out.println("sum===" + sum);
    } catch (Exception e) {
        System.out.println("In Catch");
    } finally {
        sum = a + 30;
        System.out.println("sum===" + sum);
    }
    return sum;
}

If you need to return a different value because of the error, you should be modifying sum in the catch section of your try-catch

Upvotes: 7

Prabhaker A
Prabhaker A

Reputation: 8473

Remove return form try block and add at the end of the method. Try this code

   public static int testFinnalyBlock() throws Exception{
    int a=5,b=10;
    int sum=0;
    try{
        sum = a+b;
        System.out.println("sum==="+sum);

    }catch(Exception e){
        System.out.println("In Catch");
    }finally{
        sum = a+30;
        System.out.println("sum==="+sum);
    //          return 1;
    }
    return sum;
}

use finally block for clean up activity,not for logic.It is not good practice.

Upvotes: 2

Vimal Bera
Vimal Bera

Reputation: 10497

Remove return sum from try block and you will get your desired output. like..

try{
   sum = a+b;
   System.out.println("sum==="+sum);
}
catch(Exception e){
   System.out.println("In Catch");
}
finally{
   sum = a+30;
   System.out.println("sum==="+sum);
}
return sum;

Upvotes: 0

Related Questions