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堆栈溢出

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值