java - Return type with try-catch-finally -
this question has answer here:
i have tried following code snippet:
private integer getnumber() { integer = null; try { = new integer(5); return i; } catch(exception e) { return 0; } { = new integer(7); } } this method returns 5 , not 7.
why returns 5 , not 7?
thanks in advance.
this happens because finally block of try..catch..finally runs after code within try..catch has completed (be or not); in case of code when return i has occured.
because of behaviour value of i has been placed return variable of method before assign new value of 7 it. following work:
private integer getnumber(){ integer = null; try { = new integer(5); } catch(exception e){ = new integer(0); } finally{ = new integer(7); } return i; }
Comments
Post a Comment