Created
April 9, 2011 04:00
-
-
Save googya/911089 to your computer and use it in GitHub Desktop.
try--catch--finally中的return 和finally的执行顺序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hoodoo.SimpleException; | |
/** | |
* Created by IntelliJ IDEA. | |
* User: Hoodoo | |
* Date: 11-4-9 | |
* Time: 上午11:35 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
import static java.lang.System.*; | |
public class MultipleReturns { | |
public static int f(int i){ | |
out.println("Initialization that requires cleanup"); | |
try{ | |
out.println("Point 1"); | |
if (i==1)return 1; | |
out.println("Point 2"); | |
if (i==2)return 2; | |
out.println("Point 3"); | |
if (i==3)return 3; | |
out.println("end"); | |
return 4; | |
}finally { | |
out.println("Performing cleanup"); //finally是在return之前执行的 | |
// return 5; | |
} | |
} | |
public static void main(String[] args) { | |
for(int i=1;i<=4;i++){ | |
System.out.println(f(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment