测试案例一
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(test());
}
static int test() {
int x = 1;
try {
return x;
} finally {
++x;
System.out.println(x);
System.out.println("+++++");
}
}
}
main方法执行结果:
2
+++++
1
测试案例二
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(test());
}
static int test() {
int x = 1;
try {
return x;
} finally {
++x;
System.out.println( x);
System.out.println("+++++");
return x;
}
}
}
main方法执行结果:
2
+++++
2
测试案例三
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
badMethod();
System.out.println("A");
} catch (Exception e) {
// TODO: handle exception
System.out.println("C");
} finally {
System.out.println("B");
}
System.out.println("D");
}
static void badMethod() {
throw new Error();
}
}
main方法执行结果:
B
Exception in thread “main” java.lang.Error
at Test.badMethod(Test.java:25)
at Test.main(Test.java:13)
测试案例四
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
i = badMethod();
System.out.println(i);
}
static int badMethod() {
try {
System.out.println("A");
throw new Exception();
} catch (Exception e) {
// TODO: handle exception
System.out.println("C");
System.exit(0);
return 0;
} finally {
System.out.println("B");
return 1;
}
}
}
main方法执行结果:
A
C