package com.imooc.test;
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 result1=tct.test2();
System.out.println("test2()方法执行完毕!"+result1);
}
/**
* test()方法说明:
* divider(除数)
* result(结果)
* try-catch补货while循环
* 每次循环divider减一,result=result+100/divider
* 如果:捕获异常,打印输出"抛出异常了!!!",返回 -1
* 否则正确返回 result
*/
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;
}
}
/**
* test2()方法说明:
* divider(除数)
* result(结果)
* try-catch补货while循环
* 每次循环divider减一,result=result+100/divider
* 如果:捕获异常,打印输出"抛出异常了!!!",返回 -1
* 否则正确返回 result
*/
public int test2(){
int divider=10;
int result1=100;
try {
while(divider>-1){
divider--;
result1=result1+100/divider;
}
return result1;
}catch (Exception e) {
e.printStackTrace();
System.out.println("循环抛出异常了!!!");
return 999;
}finally{
System.out.println("这是finally!!哈哈!");
System.out.println("我是result!!我的值是:"+result1);
}
}
}
第58行代码,return result = 999;
你们看我的值 居然是381