【java】try,catch,finally 各种情况是否被执行到

 

finally会被执行到

1.catch中 throw

       try{
           int i = 1/0;
       }catch (Exception e) {
           throw e;

       }finally {
           System.out.println("finally");
       }    

 

 

2.catch中return

    try{
           int i = 1/0;
       }catch (Exception e) {
           return;

       }finally {
           System.out.println("finally");
       }

 

 

 

 

 

finally不会被执行到

1.catch中退出程序

    try{
           int i = 1/0;
       }catch (Exception e) {
           System.exit(1);

       }finally {
           System.out.println("finally");
       }

 

posted @ 2021-01-20 13:31  Angel挤一挤  阅读(144)  评论(0)    收藏  举报