public class StrMain {
public static void main(String[] args) {
System.out.println(test("123a"));
System.out.println("-----------------");
System.out.println(test("456"));
}
public static int test(String str){
if("".equals(str))
return 0;
int num = 1111;
try{
num = Integer.parseInt(str);
System.out.println(0);
return num;
}catch(NumberFormatException e){
num = Integer.parseInt(str.substring(0, str.length()-1));
System.out.println(1);
}finally{
System.out.println(2);
}
System.out.println(3);
return num;
}
}
执行顺序:把一个string类型的数字字符串转化为int类型,由于"123a"结尾字符不是数字,try执行到抛出异常处,会抛出非数字字符异常,进入catch语句,catch语句块中没有return语句时,会先执行完catch语句块,再执行finally语句块。再执行最外边的语句读到return语句返回结束test方法。
“456”是单纯的数字字符串,不会抛出异常,会执行完try语句块,这里要主要的是try在执行到return语句时,会跳到finally语句块执行完finally中的语句,再返回return 中的值。
执行结果:
1
2
3
123
-----------------
0
2
456
public class intMain {
public static void main(String[] args) {
System.out.println(test());
}
public static int test(){
int b = 20;
try{
System.out.println("try block");
return b+=80;
}catch(Exceptione){
System.out.println("catch block");
}finally{
System.out.println("finally block");
if(b>25)
System.out.println("b>25,b = "+b);
}
return 0;
}
}
finally语句会在try return执行之后执行,如果finally中没有return,会返回try中原先的return值。
执行结果:
try block
finally block
b>25,b = 100
100
public class FinallyTest {
public static void main(String[] args) {
System.out.println(test1());
}
public static String test1(){
try{
System.out.println("test1try block");
return test2();
}catch(Exceptione){
System.out.println("test1 catch block");
}finally{
System.out.println("test1 finally block");
}
return "test";
}
public static String test2(){
System.out.println("run test2");
return "Hello test2";
}
}
执行结果:
test1 try block
run test2
test1 finally block
Hello test2
public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test());
}
public static String test(){
try{
System.out.println("tryblock");
return testT();
}catch(Exception e){
System.out.println("catchblock");
return "ccc";
}finally{
System.out.println("finallyblock");
return testF();
}
}
public static String testF(){
System.out.println("runtestF");
return "hellotestF";
}
public static String testT(){
System.out.println("runtestT");
return "hellotestT";
}
}
try运行到return语句时,先运行try的return语句,然后运行finally语句块,由于finally中存在return,故从这里return方法test的返回值。
运行结果:
try block
run testT
finally block
run testF
hello testF
public class Student {
public String name;
public String age;
}
public class FinallyTest2 {
public static void main(String[] args) {
System.out.println(test());
}
public static Student test(){
Student stu = new Student();
try{
stu.name = "A";
stu.age = "10";
return stu;
}catch(Exception e){
}finally{
stu.name = "B";
System.out.println(stu);
}
return null;
}
}
这个测试是为了证明当方法的返回类型为实体时,return所返回的是地址,而不是当返回值为基本类型时的值引用。
有return的try catch finally 执行顺序:
1.finally语句块一定会被执行。
2.try中没有return,也没有报错的话,会执行完try语句执行finally语句。如果有return语句 ,先执行完return中的语句块,但不返回结果,再执行finally中的语句块,再返回try中的return结果。
3.try中抛出异常,会在出现异常的地方转入catch语句块,与2类似。
4.如果finally语句块中有return,则方法从此处结束,一般不建议这么做。
5.方法的返回如果是基本类型则为返回时是值传递,比如try中有return,那么在finally中对于参数的修改不影响返回值,如果是类类型返回的是对应实体的地址,也就是说try中返回的对象会受到finally语句块修改的影响。