循环和try ... catch 块的关系:
public class ForException {
/*
* 1 2 5 java.lang.ArithmeticException: / by zero at
* exception.ForException.forException1(ForException.java:13) at
* exception.ForException.main(ForException.java:5) -5
* ----------------
* 1 2
* 5 java.lang.ArithmeticException: / by zero at
* exception.ForException.forException2(ForException.java:23) at
* exception.ForException.main(ForException.java:7)
*
*/
public static void main(String[] args) {
forException1();
System.out.println("----------------");
new ForException().forException2();
}
public static void forException1() {
for (int i = 5; i > 0; i--) {
try {
System.out.println(5 / (i - 2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void forException2() {
try {
for (int i = 5; i > 0; i--) {
System.out.println(5 / (i - 2));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}