zoukankan      html  css  js  c++  java
  • java 异常捕捉 ( try catch finally ) 你真的掌握了吗?


    前言:
    java 中的异常处理机制你真的理解了吗?掌握了吗?
    catch 体里遇到 return 是怎么处理? finally 体遇到 return 怎么办?finally 体里有 System.exit() 方法怎么处理?当 catch 和 finally 体里同时遇上 return 怎么办?

    相信你在处理异常的时候不是每次都把它 throws 掉就完事了,很多时候异常是需要我们自己来 catch 并针对所抛出的 Exception 做一些后续的处理工作。

    直接上代码,先贴下面测试需要调用的方法:
     1
     2    // catch 后续处理工作
     3    public static boolean catchMethod() {
     4        System.out.print("call catchMethod and return  --->>  ");
     5        return false;
     6    }

     7    // finally后续处理工作
     8    public static void finallyMethod() {
     9        System.out.println();
    10        System.out.print("call finallyMethod and do something  --->>  ");
    11    }

    12


    1. 抛出 Exception,没有 finally,当 catch 遇上 return
     1
     2public static boolean catchTest() {
     3        try {
     4            int i = 10 / 0;   // 抛出 Exception,后续处理被拒绝
     5            System.out.println("i vaule is : " + i);
     6            return true;    // Exception 已经抛出,没有获得被执行的机会
     7        }
     catch (Exception e) {
     8            System.out.println(" -- Exception --");
     9            return catchMethod();    // Exception 抛出,获得了调用方法并返回方法值的机会
    10        }

    11    }

    12

    后台输出结果:
    1
    2 -- Exception --
    3call catchMethod and return  --->>  false
    4

    2. 抛出 Exception,当 catch 体里有 return,finally 体的代码块将在 catch 执行 return 之前被执行
     1
     2public static boolean catchFinallyTest1() {
     3        try {
     4            int i = 10 / 0// 抛出 Exception,后续处理被拒绝
     5            System.out.println("i vaule is : " + i);
     6            return true;   // Exception 已经抛出,没有获得被执行的机会
     7        }
     catch (Exception e) {
     8            System.out.println(" -- Exception --");
     9            return catchMethod();  // Exception 抛出,获得了调用方法的机会,但方法值在 finally 执行完后才返回
    10        }
    finally{
    11            finallyMethod();  // Exception 抛出,finally 代码块将在 catch 执行 return 之前被执行
    12        }

    13    }

    14

    后台输出结果:
    1
    2 -- Exception --
    3call catchMethod and return  --->>  
    4call finallyMethod and do something  --->>  false
    5

    3. 不抛 Exception,当 finally 代码块里面遇上 return,finally 执行完后将结束整个方法
     1
     2public static boolean catchFinallyTest2() {
     3        try {
     4            int i = 10 / 2;  // 不抛出 Exception
     5            System.out.println("i vaule is : " + i);
     6            return true;   // 获得被执行的机会,但执行需要在 finally 执行完成之后才能被执行
     7        }
     catch (Exception e) {
     8            System.out.println(" -- Exception --");
     9            return catchMethod();
    10        }
    finally{
    11            finallyMethod();
    12            return false// finally 中含有 return 语句,这个 return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,方法到此结束,返回 false
    13        }

    14    }

    15

    后台输出结果:
    1
    2i vaule is : 5
    3
    4call finallyMethod and do something  --->>  false
    5

    4. 不抛 Exception,当 finally 代码块里面遇上 System.exit() 方法 将结束和终止整个程序,而不只是方法
     1
     2public static boolean finallyExitTest() {
     3        try {
     4            int i = 10 / 2;  // 不抛出 Exception
     5            System.out.println("i vaule is : " + i);
     6            return true;   // 获得被执行的机会,但由于 finally 已经终止程序,返回值没有机会被返回
     7        }
     catch (Exception e) {
     8            System.out.println(" -- Exception --");
     9            return true;
    10        }
    finally {
    11            finallyMethod();
    12            System.exit(0);// finally 中含有 System.exit() 语句,System.exit() 将退出整个程序,程序将被终止
    13        }

    14    }

    15

    后台输出结果:
    1
    2i vaule is : 5
    3
    4call finallyMethod and do something  --->>  
    5

    5. 抛出 Exception,当 catch 和 finally 同时遇上 return,catch 的 return 返回值将不会被返回,finally 的 return 语句将结束整个方法并返回
     1
     2public static boolean finallyTest1() {
     3        try {
     4            int i = 10 / 0// 抛出 Exception,后续处理被拒绝
     5            System.out.println("i vaule is : " + i);
     6            return true;   // Exception 已经抛出,没有获得被执行的机会
     7        }
     catch (Exception e) {
     8            System.out.println(" -- Exception --");
     9            return true;  // Exception 已经抛出,获得被执行的机会,但返回操作将被 finally 截断
    10        }
    finally {
    11            finallyMethod();
    12            return false;  // return 将结束整个方法,返回 false
    13        }

    14    }

    15

    后台输出结果:
    1
    2 -- Exception --
    3
    4call finallyMethod and do something  --->>  false
    5

    6. 不抛出 Exception,当 finally 遇上 return,try 的 return 返回值将不会被返回,finally 的 return 语句将结束整个方法并返回
     1
     2public static boolean finallyTest2() {
     3        try {
     4            int i = 10 / 2;  // 不抛出 Exception
     5            System.out.println("i vaule is : " + i);
     6            return true;   // 获得被执行的机会,但返回将被 finally 截断
     7        }
     catch (Exception e) {
     8            System.out.println(" -- Exception --");
     9            return true;
    10        }
    finally {
    11            finallyMethod();
    12            return false// return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,返回 false
    13        }

    14    }

    15

    后台输出结果:
    1
    2i vaule is : 5
    3
    4call finallyMethod and do something  --->>  false
    5

    结语:
    (假设方法需要返回值)
    java 的异常处理中,
    在不抛出异常的情况下,程序执行完 try 里面的代码块之后,该方法并不会立即结束,而是继续试图去寻找该方法有没有 finally 的代码块,
    如果没有 finally 代码块,整个方法在执行完 try 代码块后返回相应的值来结束整个方法;
    如果有 finally 代码块,此时程序执行到 try 代码块里的 return 语句之时并不会立即执行 return,而是先去执行 finally 代码块里的代码,
    若 finally 代码块里没有 return 或没有能够终止程序的代码,程序将在执行完 finally 代码块代码之后再返回 try 代码块执行 return 语句来结束整个方法;
    若 finally 代码块里有 return 或含有能够终止程序的代码,方法将在执行完 finally 之后被结束,不再跳回 try 代码块执行 return。
    在抛出异常的情况下,原理也是和上面的一样的,你把上面说到的 try 换成 catch 去理解就 OK 了 *_*





      
    posted on 2012-07-08 09:45 fancydeepin 阅读(56234) 评论(9)  编辑  收藏

    总结


    java中当a() throws Exception;在别的方法中调用的时候用try...catch...中可以在try和catch中都加入return语句,会按需返回;
  • 相关阅读:
    User Profile Service 应用程序的新增功能
    内容管理互操作性服务 (CMIS) 连接器概述 (SharePoint Server 2010)
    AD二次开发提示“出现了一个操作错误”的解决办法
    Excel2010与MOSS2010数据同步配置
    InfoPath2010表单IE浏览器2个“微型内嵌工具”的使用和介绍
    洗礼灵魂,修炼python(1)python简介
    洗礼灵魂,修炼python(2)python安装和配置
    C语言标准定义的32个关键字
    【学习笔记】.NET知识体系结构
    【读】自定义博客园皮肤:暗色流体响应式布局
  • 原文地址:https://www.cnblogs.com/jpfss/p/8624825.html
Copyright © 2011-2022 走看看