标签:直接 ati except print main ++ ase int div
1、循环中异常使用
package com.an.exception;
/**
* @description:
* @author: anpeiyong
* @date: Created in 2020/1/16 10:18
* @since:
*/
public class ForExceptionTest {
public static void main(String[] args) {
test1();
}
public static void test1(){
for (int i=0;i<4;i++){
try {
System.out.println("第"+i+"次循环");
if (i==2){
int i1=1/0;
}
}catch (Exception e){
System.out.println("第"+i+"次循环出现异常:"+e);
//使用break; 直接结束循环
/** 结果:
*第0次循环
* 继续执行...
* 第1次循环
* 继续执行...
* 第2次循环
* 第2次循环出现异常:java.lang.ArithmeticException: / by zero
*/
// break;
//无论是否使用continue; 都会结束当前循环、进入下次循环
/** 结果:
*第0次循环
* 继续执行...
* 第1次循环
* 继续执行...
* 第2次循环
* 第2次循环出现异常:java.lang.ArithmeticException: / by zero
* 继续执行...
* 第3次循环
* 继续执行...
*/
}
System.out.println("继续执行...");
}
}
}
标签:直接 ati except print main ++ ase int div
原文地址:https://www.cnblogs.com/anpeiyong/p/12201699.html