慕婉清4106374
2018-05-21 16:10
代码如下:
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
因为
at com.imooc.test.TryCatchTest.test(TryCatchTest.java:17)
at com.imooc.test.TryCatchTest.main(TryCatchTest.java:8)
这两句并不会在控制台严格按照顺序输出~它们会很任性地随机在某个位置出现
Java入门第三季
409788 学习 · 4340 问题
相似问题