????Java?е?try-catch-finally????return
???????????? ???????[ 2012/8/20 10:32:02 ] ????????
?????????return???????????????????????finally???????return????????finally??return??????????????棬???finally??к???????
?????????finally???治?????return????????????return?????????try??catch??????????????е??????????
????1??return????????????????Ρ?
????2??return??????try??catch???涼?????
????3??return??????try?????????????
????4??return??????catch?????????????
???????????????????????????????е?????????????
??????1????????????е?try???????????????????÷???2???????????????test0_1????????????У?????2??4??????е?????????????2????
??????2????????????е?catch??????????????????????????к??????????????????4???????????????е?test0?????????????????У??????????4??
??????3????????????е?try??catch????????????к???????????????1?????????????test0_2?????????????÷???1????
????????????????
public class Test {
public static void main(String[] args) {
System.out.println("=============test1==================");
System.out.println(test1());
System.out.println("===============================");
System.out.println("=============test1_1==================");
System.out.println(test1_1());
System.out.println("===============================");
System.out.println("
============test2===================");
System.out.println(test2());
System.out.println("===============================");
System.out.println("
============test2_1===================");
System.out.println(test2_1());
System.out.println("===============================");
System.out.println("
============test3===================");
System.out.println(test3());
System.out.println("===============================");
System.out.println("
============test3_1===================");
System.out.println(test3_1());
System.out.println("===============================");
}
public static String test0() {
String a;
int b;
try{
b = 8/0;
}catch(Exception e){
return null;
}
a = String.valueOf(b);
return a+b;
}
public static String test0_1() {
String a;
int b;
try{
b = 8/0;
a = String.valueOf(b);
return a+b;
}catch(Exception e){
return null;
}
//return a+b;
}
public static String test0_2() {
String a;
int b=0;
try{
b = 8/0;
}catch(Exception e){
}
a = String.valueOf(b);
return a;
}
public static String test1() {
String a = "in try";
int n = -1;
try{
return a+n; //???????????????finally
} catch ( Exception e ) {
} finally {
//??String??int????????Ч
a = "in finally";
n = 123;
System.out.println("do finally");
}
return a; //???????
} //???????return???????????????????????finally???????return????????finally
public static String test1_1() {
String a = "in try";
try{
return a;
} catch ( Exception e ) {
} finally { //??eclpise????????????finally???治??????return???
a = "in finally";
System.out.println("do finally");
return a; //???????eclipse?????????
}
}
public static int test2() {
int a = 1;
try{
return a;
} catch ( Exception e ) {
} finally {
a = 2;
System.out.println("do finally");
}
return a;
} //???????finally?????????Ч?????????a=1
public static int test2_1() {
int a = 1;
try{
return a;
} catch ( Exception e ) {
} finally {
a = 2;
System.out.println("do finally");
return a;
}
} //???????a?finally????????a=2
//Helper??????????????????
static class Helper {
int a;
public String toString() {
return String.valueOf(a);
}
}
public static Helper test3() {
Helper h = new Helper();
h.a = 1;
try{
return h;
} catch ( Exception e ) {
} finally {
h.a = 2; //??h.a???????????!!
//?????try???淵??????????????????????????? ????????
System.out.println("do finally");
}
return h; //??????????
}
public static Helper test3_1() {
Helper h = new Helper();
h.a = 1;
try{
return h;
} catch ( Exception e ) {
} finally {
h.a = 2; //????a=2????????????
System.out.println("do finally");
return h;
}
}
/**
* ???
* return???finally???治?????return???????????????????try??catch????
*
*/
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11