• try/catch/finally的各种情况


    众所周知,try语句报错,会执行catch语句,然后执行finally,以下这几种情况,看看会如何输出。

    1、try语句中包含return,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "111";
    8. } catch (Exception e) {
    9. throw new NullPointerException();
    10. } finally {
    11. System.out.println(123);
    12. }
    13. }

    结果:finally正常输出

     2、try语句报错,catch语句抛出错误,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. throw new NullPointerException();
    10. } finally {
    11. System.out.println(123);
    12. }
    13. }

    结果:finally执行成功,再抛出catch中的错误

     3、try语句报错,catch包含return,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. System.out.println(123);
    13. }
    14. }

    结果:catch中的语句输出成功,finally中的语句输出成功

     4、try和finally中的语句都包含输出

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "123";
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. return "789";
    13. }
    14. }

    结果:finally成功return

     5、try报错,catch和finally包含return语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. return "789";
    13. }
    14. }

    结果:finally成功return

     6、try包含return,finally抛出错误

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "789";
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. throw new NullPointerException();
    13. }
    14. }

    结果:直接报错

     7、try包含报错,catch包含return,finally抛出错误

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new RuntimeException();
    8. } catch (Exception e) {
    9. return "456";
    10. } finally {
    11. throw new NullPointerException();
    12. }
    13. }

    结果:直接报错

    通过上面的例子,我们可以得到以下结论:

    1. finally语句存在的话,一定会执行
    2. 若try语句报错,会执行catch语句,如果catch语句也报错,则会先执行finally语句,再抛出catch的错误
    3. try语句或者catch语句return了,finally仍然会执行
    4. 若finally语句中包含return,则会覆盖try或者catch中的return
    5. 若finally语句报错,这个错误一定会抛出,终止程序
  • 相关阅读:
    MySQL中json_extract函数说明
    鸿蒙入门-13Gauge组件
    如何运用java代码操作Redis
    单片机IAP固件升级分几步?(Qt上位机)
    向Zookeeper中注册内容以及从zookeeper中发现内容
    redis -- 基本介绍 -- 字符串、列表、集合、有序集合、哈希
    手机打开 第三方 “微信、快手、QQ、电话、信息” 等
    软件测试面试必问:为什么要选择软件测试?
    [山东科技大学OJ]2047 Problem E: 坏了哪些键
    存储架构颠覆者Optane持久性内存
  • 原文地址:https://blog.csdn.net/qq_49721447/article/details/127983337