-2

I was trying exception handling. below code i was not able to understand. Please explain how this works internally

public int method()
{
    try
    {
        return 1;
    }
    catch(Exception e)
    {
        return 2;
    }
    finally
    {
         return 3;
    }
 }

Please explain me how this works in java

  • 2
    I'm voting to close this question as off-topic because read a tutorial and indicate very precisely what isn't clear about it. http://docs.oracle.com/javase/tutorial/essential/exceptions/ – Jeroen Vannevel Mar 13 '15 at 12:40
  • Jeroen: I think his question is, it should return 1, but in theory finally always runs. Raj: in this case, the method 'll be ended by the return statement. – Stultuske Mar 13 '15 at 12:43
  • I guess this is not a duplicate question as i wanted to know internal working of this . Please go through this code try { return 1; } catch(Exception e) { return 2; } finally { System.out.println("hello"); } for above code why value 1 is return – Raj Gosaliya Mar 13 '15 at 13:50

1 Answers1

1

The method will always return 3.Because even if a return statement is there in try block the control will be passed on to finally block.And it will return 3 and the return value from try block will be lost