package imooc2;
public class TryCatchTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
TryCatchTest t = new TryCatchTest();
int result = t.test();
System.out.println("test()方法执行完毕,输出结果: "+ result);
}
/*
* 创建两个变量,divider(除数)和result(结果)
* try catch 捕获while循环
* 每次循环divider减1,result = result+100/divider
* 如果捕获异常,打印输出“抛出异常了”,return -1
* 否则返回result
*/
public int test() {
int divider =10;
int result =100;
//try {
while(divider > -1) {
divider--;
result = result+100/divider;
//}
return result;
//}catch(ArithmeticException e) {
//e.printStackTrace();
//System.out.println("循环抛出异常");
//return -1;
}
return 0;
}
}