java try catch 异常后还会继续执行吗

1、有 try-catch 语句块,并且 throw 在 catch 语句块里,那么 try 语句块中引发异常(报错)的那一行代码的后续代码都不执行并且 catch 语句块后的代码也都不执行(遇到 finally 除外)。(见情形一和情形二)

2、有 try-catch 语句块,并且 throw 在 try 语句块里,那么 try 语句块中引发异常(报错)的那一行代码的后续代码都不执行,但是 catch 语句块后的代码会继续执行。(见情形三)

3、有 try-catch 语句块,但是没有 throw 语句,那么 try 语句块中引发异常(报错)的那一行代码的后续代码都不执行,但是 catch 语句块后的代码会继续执行。(见情形四)

4、有 try-catch 语句块,但是没有 throw 语句,如果这个异常对象与catch中声明的异常类型不匹配,程序会中断。(见情形五)

5、没有 try-catch 语句块,单纯只有 throw 抛出异常的语句块,那么 throw 后面的代码都不执行。(见情形六)

情景一

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length();  //null没有length()方法,报空指针异常错误
            //下面两条赋值语句不会执行
            c = 1;  
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d);   //本条语句也不执行
    }
}

分析:null 没有 length() 方法,所以 int b = a.length() 这行代码会报空指针异常的错误,然后直接跳转到 catch 语句块去执行,打印出 c 的值依旧是 0 ,说明 c=1 没有执行故没有赋值成功,执行完 catch 里的语句后程序就结束了,System.out.println(“d的值为:” + d)这行代码是不执行的,如果想要这行代码被执行,那么可以将其放在 finally 语句块内,catch 语句块执行完后就会执行 finally 语句块。

情景二

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            if (a == null) {
                throw new RuntimeException("a的值不能是空");
            }
            //下面两条赋值语句不会执行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c); //会执行
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d); //不会执行
    }
}

分析:先抛出 if 里面的异常,跳过赋值语句的执行,直接执行 catch 里的代码,打印出 c 的初始值 0 后又接收到一次异常的抛出, 至此后续代码就不会再执行,d 的值也就不可能打印出来。

情景三

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            if (a == null) {
                throw new RuntimeException("a的值不能是空");
            }
            //下面两条赋值语句不会执行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c); //会执行
            //throw new RuntimeException(e);  //注释抛异常的函数
        } 
        System.out.println("d的值为:" + d); //会执行
    }
}

分析:在 if 里面抛出异常后不再执行后面的赋值语句,而是直接跳出 try 语句块转而进入 catch 语句块,但是该语句块中抛出异常的函数已被注释,所以程序会继续往下执行,从而打印出 c 和 d 的初始值 0 。 

情景四

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length();  //null没有length()方法,报空指针异常错误
            //下面两条赋值语句不会执行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            //throw new RuntimeException(e);   //该行注释掉
        }
        System.out.println("d的值为:" + d);   //会执行
    }
}

分析: 注释掉 throw new RuntimeException(e) 这行后,没有异常抛出,它会继续往下走,因此 d 的值能够打印出来,但是打印出来的 c 和 d 的值都是初始值 0 ,赋值语句是没有执行成功的。

情景五

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length();  //null没有length()方法,报空指针异常错误
            //下面两条赋值语句不会执行
            c = 1;
            d = 2;
        } catch (IndexOutOfRangeException e) {
            System.out.println("c的值为:" + c);
            //throw new RuntimeException(e);   //该行注释掉
        }
        System.out.println("d的值为:" + d);   //会执行
    }
}

分析: 异常类型不匹配,程序中断,c与d的值都不会打印。

情景六

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        if (a == null) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException("字符串a的值不能为空");  //throw语句不在try中
        }
        System.out.println("d的值为:" + d);   //该行代码不会执行
    }
}

分析:throw new RuntimeException(“字符串a的值不能为空”)自定义了抛出的提示信息,可看成是一个 return 返回了相应的信息,在抛出异常后其后的代码不会再执行,因此 d 的值不会打印出来。 

情景七(没有异常抛出的正常情况

public class ExceptionTest {
    public static void main(String[] args) {
        String a = "null";
        int c = 0, d = 0;
        try {
            int b = a.length();  //"null"有length()方法,正常执行
            //下面两条赋值语句会被执行
            c = 1;  
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d);   //本条语句也会被执行
    }
}

分析:将 null 改为 “null” 后,length() 方法是有效的,此时 int b = a.length() 这行代码不报错,正常执行其后的两条赋值语句,所以程序不执行 catch 中的语句,故不会打印 c 的值,然后执行 System.out.println(“d的值为:” + d) 这行代码,打印出重新赋值后的 d 的值为 2 。

catch{}常用的异常类型

Exception 描述
SystemException 其他用户可处理的异常的基本类
ArgumentException 方法的参数是非法的
ArgumentNullException 一个空参数传递给方法,该方法不能接受该参数
ArgumentOutOfRangeException 参数值超出范围
ArithmeticException 出现算术上溢或者下溢
ArrayTypeMismatchException 试图在数组中存储错误类型的对象
BadImageFormatException 图形的格式错误
DivideByZeroException 除零异常
DllNotFoundException         找不到引用的DLL
FormatException 参数格式错误
IndexOutOfRangeException 数组索引超出范围
InvalidCastException 使用无效的类
InvalidOperationException 方法的调用时间错误
MethodAccessException 试图访问思友或者受保护的方法
MissingMemberException 访问一个无效版本的DLL
NotFiniteNumberException 对象不是一个有效的成员
NotSupportedException 调用的方法在类中没有实现
NullReferenceException 试图使用一个未分配的引用
OutOfMemoryException 内存空间不够
PlatformNotSupportedException 平台不支持某个特定属性时抛出该错误
StackOverflowException 堆栈溢出

版权声明:本文为博主作者:qq_41482600原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/qq_41482600/article/details/134304659

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2024年4月22日
下一篇 2024年4月22日

相关推荐