Saturday, 16 May 2015

Java's finally block with break and continue

Java's return doesn't always. By using finally clauses, control flow may do very surprising things. Consider this loop:
       for(;;) {           
           try {
              return 1;
           } finally {
               break;    
           }     
       }     
       return -1;    
   
What value does this snippet return? -1. Surprised? How about this little gem:
       while (true) {                
           try {                     
               return;               
           } finally {               
               continue;             
           }                         
       }