Thursday, September 27, 2012

Java Tidbits ............................2 (Finally)


will finally execute when return / System.exit(0) is invoked in try or catch block?

Yes . with return finally block will execute before returning the value but with System.exit(0) it immediately quits JVM so no point of execution any furthur 

------------------------------------------------------------------------------------------

BEWARE , Finally is not guaranteed to complete ! It all depends on user's implementation.
see below example ::


public class OverAndOver {
 static String s = "";
 public static void main(String[] args) {
 try {
 s += "1";
     throw new Exception();
   } catch (Exception e) { s += "2";
     } finally { s += "3"; doStuff(); s += "4";
     }
 System.out.println(s);
 }
 static void doStuff() { int x = 0; int y = 7/x; }
 }


No comments: