package imooc_exception_demo;
public class TryCatchText {
public static void main(String[] args) {
TryCatchText tc = new TryCatchText();
int result = tc.text();
int result2 = tc.text2();
System.out.println("方法执行完毕,返回值为:" + result);
System.out.println("text2执行完毕");
}
/*
devider(除数)result(结果)
try-catch捕获while循环
每次循环devider-1次,result+=100/devider
若有异常打印抛出异常了返回-1否则返回result
*/
public int text() {
int devider = 10;
int result = 100;
try {
while (devider >= -1) {
devider--;
result = result + 100 / devider;
}
return result;
} catch (Exception e) {
System.out.println("输出异常了");
return -1;
}
}
/*
devider(除数)result(结果)
try-catch捕获while循环
每次循环devider-1次,result=999
若有异常打印抛出异常了返回-1否则返回result
finally:这是finally!!,输出result
*/
public int text2() {
int devider = 10;
int result = 100;
try {
while (devider >= -1) {
devider--;
result = result + 100 / devider;
}
return result;
} catch (Exception e) {
System.out.println("输出异常了");
return result = 999;
}
}
finally
{
System.out.println("这是finally!!");
System.out.println("我是result,我的值是:" +result );
}
}