Sunday, August 20, 2017

Try Catch Finally (which return will be execute when there is no exception in try block?)

Question by : Mahesh
Solution by :Anand



In below scenario which return will be execute when there is  no exception in try block?
try{ 
  //no exception in try block
return "test1"; 
  } 
  catch(Exception e){
return "test12";

  finally{
return "test3";
output=?

/*
*  Whenever there is a return in try the control / priority passes on to finally.. But again in finally there is
     a  return so try is never.. Executed..

*   Simple thing that we need to try is replace return in finally with system. Out. Println.. then try will also            be  printed

*  as given in the example when there is return in finally that is the last thing and control will not go back
   to any caller

*  1> if der is return in finally , den finally return only executes and try return will be skipped.
     2>if der is no return in finally , den try return will always be executed.






0 comments:

Post a Comment