/**
* @author qing
*
* Try……catch……finally中return的测试
*/
public class TryTest {
/**
* 主要方法
*/
public static void main(String[] args) {
// 调用 测试方法
String result = get();
// 打印 测试方法返回的结果
System.out.println(result);
}
public static String get() {
int value = 0;
try {
System.out.println("try……");
//等式1/0 :分母为0 的明显错误 ——制造错误(用于抛异常)
int result = 1 / value;
return "111";
} catch (Exception e) {
System.out.println("catch……");
return "444";
} finally {
System.out.println("finally……");
return "333";
}
// return "222";
}
}
try{}、catch{}、finally{} 代码块中,都有 return 时:
以 finally{} 中的 return 为准,其他的都将失效,return 之前的代码都有效。
catch{}、finally{} 中,都没有 return 时
程序中 try{} 内部没有异常的情况下,若有 finally{},且 finally{} 中没有 return. 若在 try{} 中遇到 return,则先跳去执行 finally{} 中的代码,在回来执行 try{} 中 return.
————————————————
https://blog.csdn.net/wangqingbo0829/article/details/52458283