场景:循环输出1-10,当i=5的时候出现异常,此时想让程序继续执行。
catch 里面记录错误日志,continue继续执行for循环。
public static void main(String[] args) throws Exception {
try {
for(int i=0;i<=10;i++){
try {
if(i==5){
throw new RuntimeException("执行出错了");
}
}catch (Exception e){
System.out.println(e.getMessage()+"记录日志");
continue;
}
System.out.println("正在执行......"+i);
}
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
System.out.println("关闭数据库链接");
}
}