• 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语句报错,这个错误一定会抛出,终止程序
  • 相关阅读:
    广东全链功能集群 林裕豪:解读建设农业龙头企业总部基地
    Windows电脑10实用小技巧
    新型跨境电商平台如何选择?新手做跨境电商如何起步?
    笔试刷题汇总
    C语言笔记(六)
    半桥BUCK电路—记录篇
    MYSQL--索引
    雪花算法生成主键ID
    【办公自动化】在Excel中按条件筛选数据并存入新的表2.0(文末送书)
    元宇宙基建狂魔?Cocos v3.6 正式发布功能大更新
  • 原文地址:https://blog.csdn.net/qq_49721447/article/details/127983337