Packagecn.itcast_07;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;/** Finally: the body of the statement that is finally controlled must be executed * Note: If the JVM exits before executing to finally, it cannot be executed. * * A: Format * try...catch...finally ... * B: For freeing up resources, meeting in IO Flow operations and database operations*/ Public classFinallydemo { Public Static voidMain (string[] args) {String s= "2014-11-20"; SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Date D=NULL; Try { //System.out.println (10/0);D =Sdf.parse (s); } Catch(ParseException e) {e.printstacktrace (); System.exit (0); } finally{System.out.println ("The code here can be executed."); } System.out.println (d); }}
Here is the result of the JVM exiting the execution:
1 Packagecn.itcast_07;2 3 /*4 * Interview questions:5 * The difference between 1:final,finally and finalize6 * Final: final meaning, can be modified class, member variable, member method7 * Modifier class, class cannot be inherited8 * modifier variable, variable is constant9 * Modification method, method cannot be overriddenTen * Finally: is part of exception handling and is used to release resources. One * In general, the code will definitely execute, special cases: the JVM exits before executing to finally A * Finalize: Is a method of the object class for garbage collection - * - * 2: If there is a return statement inside the catch, will the code in finally be executed ? the * If yes, please ask before return, or after return. - * will. Ago. - * - * Accurately speaking, should be in the middle. + * - * 3:try...catch...finally format variants + * a:try...catch...finally A * B:try...catch at * C:try...catch...catch ... - * d:try...catch...catch...finally - * e:try...finally - * This practice is currently in order to release resources. - */ - Public classFinallyDemo2 { in Public Static voidMain (string[] args) { - System.out.println (GetInt ()); to } + - Public Static intgetInt () { the intA = 10; * Try { $System.out.println (a/0);Panax NotoginsengA = 20; -}Catch(ArithmeticException e) { theA = 30; + returnA; A /* the * Return a when the program executes to this step, this is not return a but return 30; The return path is formed. + * But, it found that there is finally, so continue to execute finally content, a=40 - * Go back to the previous return path again and continue to return; $ */ $}finally { -A = 40; - returnA//if that turns out to be 40. the } - //return A;Wuyi } the}
Android (Java) Learning notes 83:finally the role of keywords