其他
面试官:try catch应该在for循环里面还是外面?
public static void tryOutside() {
try {
for (int count = 1; count <= 5; count++) {
if (count == 3) {
//故意制造一下异常
int num = 1 / 0;
} else {
System.out.println("count:" + count + " 业务正常执行");
}
}
} catch (Exception e) {
System.out.println("try catch 在for 外面的情形, 出现了异常,for循环显然被中断");
}
}
专属福利
public static void tryInside() {
for (int count = 1; count <= 5; count++) {
try {
if (count == 3) {
//故意制造一下异常
int num = 1 / 0;
} else {
System.out.println("count:" + count + " 业务正常执行");
}
} catch (Exception e) {
System.out.println("try catch 在for 里面的情形, 出现了异常,for循环显然继续执行");
}
}
}
Runtime runtime = Runtime.getRuntime();
long memory = runtime.freeMemory();
我就知道你会点赞+“在看”