Java中异常问题(异常抛出后是否继续执行的问题)

public static void test() throws Exception  {
        throw new Exception("参数越界");
        System.out.println("异常后"); //编译错误,「无法访问的语句」
    }

//代码2
//异常被捕获,日志打印了异常,代码继续执行
        try{
            throw new Exception("参数越界");
        }catch(Exception e) {
            e.printStackTrace();
        }
        System.out.println("异常后");//可以执行
//psvm   快捷键
    public static void main(String[] args) {
        try {
            test();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test() throws Exception  {
        //代码3
        if(true) {
            throw new Exception("参数越界");
        }
        System.out.println("异常后"); //抛出异常,不会执行
    }
public static void test() throws Exception  {
        //代码4
        try {
            int i=1/0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("代码执行异常后打印并抛出异常提示");
        }
        System.out.println("异常后"); //抛出异常,不会执行
    }




//打印日志
java.lang.ArithmeticException: / by zero
	at zmc.eter.etern.text.text.test(text.java:23)
	at zmc.eter.etern.text.text.main(text.java:14)
java.lang.Exception: 代码执行异常后打印并抛出异常提示
	at zmc.eter.etern.text.text.test(text.java:26)
	at zmc.eter.etern.text.text.main(text.java:14)
原创文章 75 获赞 28 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_33999844/article/details/88549982