码迷,mamicode.com
首页 > 编程语言 > 详细

JavaSE---异常---扩展

时间:2020-01-16 16:07:58      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:直接   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("继续执行...");
        }
    }
}

  

JavaSE---异常---扩展

标签:直接   ati   except   print   main   ++   ase   int   div   

原文地址:https://www.cnblogs.com/anpeiyong/p/12201699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!