3

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.

Hardik Patel
  • 937
  • 3
  • 14
  • 39

5 Answers5

7

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

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.

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
0

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;
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
  • That is wrong, the exception is not the problem. If he write a return into a black(here try) the program stops and return and is not able to reach the finally block. ^^ – Gerret Aug 28 '13 at 05:54
0

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;
    }

}
codeMan
  • 5,730
  • 3
  • 27
  • 51
Simmant
  • 1,477
  • 25
  • 39
  • I think if you do it like so, you have the same problem. After the first return the method exits! – Gerret Aug 28 '13 at 06:27
0

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;
}
Shashi
  • 1,165
  • 5
  • 20
  • 39