为什么这节我运行的结果是先出现异常,再出现运行结果?
代码如下:
package com.imooc.test;
public class TryCatchTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
TryCatchTest a=new TryCatchTest();
int result=a.test();
System.out.println("test()方法,执行完毕!返回值为:"+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;
}
}
}
运行结果为:
java.lang.ArithmeticException: / by zero
at com.imooc.test.TryCatchTest.test(TryCatchTest.java:17)
at com.imooc.test.TryCatchTest.main(TryCatchTest.java:8)
循环抛出异常了!!!
test()方法,执行完毕!返回值为:-1