qq_慕婉清0460615
2020-04-25 10:47
package com.imooc.test;
import javax.naming.spi.DirStateFactory.Result;
public class TryCatchTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
TryCatchTest tct = new TryCatchTest();
int result = tct.test();
System.out.println("test()方法执行完毕!返回值为:"+result);
int result2 = tct.test();//tct是TryCatchTest类的新建实例化对象
System.out.println("我想大声告诉你,test2()方法执行完毕!返回值为:");
}
public int test(){
int divider = 10;
int result = 100;
try {
while (divider > -1) {
divider--;
result =result + 100/divider;
} return result;
} catch (Exception e) {
e.printStackTrace();//
System.out.println("循环抛出异常了!");
return-1;
}
}
public int test2(){
int divider = 10;
int result = 100;
try {
while (divider > -1) {
divider--;
result =result + 100/divider;
} return result;
} catch (Exception e) {
e.printStackTrace();//
System.out.println("循环抛出异常了!");
return result=999;
}finally {
System.out.println("这是finally,哈哈");
System.out.println("我是result,我的值是"+result);
}
}
}
你函数调用写错了,main方法的第二个调用应该是test2()。
Java入门第三季
409792 学习 · 4340 问题
相似问题