有关try..catch..finally处理异常的总结

简介:

//看一下下面的程序,你能正确的写出不同的testEx2()方法时,程序的最终打印出来的数据吗....先不要看下面的答案
public class ExceptionTest { 
    public ExceptionTest() { 
    } 
   
    boolean testEx() throws Exception { 
        boolean ret = true; 
        try { 
            ret = testEx1(); 
        } catch (Exception e) { 
            System.out.println("testEx, catch exception"); 
            ret = false; 
            throw e; 
        } finally { 
            System.out.println("testEx, finally; return value=" + ret); 
            return ret; 
        } 
    } 
   
    boolean testEx1(){ 
        boolean ret = true; 
        try { 
            ret = testEx2();
            if(!ret){
           return false;
        }
            System.out.println("testEx1, at the end of try"); 
            return ret;
          
        } catch (Exception e) { 
            System.out.println("testEx1, catch exception"); 
            ret = false; 
            throw e; 
        } finally { 
            System.out.println("testEx1, finally; return value=" + ret); 
            return ret; 
        } 
    } 
    //第一种:
   /* boolean testEx2() throws Exception{ 
        boolean ret = true; 
        try { 
 
            int b = 12; 
            int c; 
            for (int i = 2; i >= -2; i--) { 
                c = b / i;
                System.out.println("i=" + i); 
            } 
            return ret; 
        } catch (Exception e) { 
            System.out.println("testEx2, catch exception"); 
            ret = false; 
            throw e;
        } finally { 
            System.out.println("testEx2, finally; return value=" + ret); 
            return ret; 
        }
    } */
    
     
    //第二种:
     boolean testEx2() throws Exception { 
        boolean ret = true;  
            int b = 12; 
            int c; 
            for (int i = 2; i >= -2; i--) { 
                c = b / i; 
                System.out.println("i=" + i); 
            } 
            System.out.printf("这句话打打出来了吗??????");
            return true; 
      }
     
 
    //第三种:
    /*boolean testEx2() throws Exception{ 
        boolean ret = true; 
        try { 
 
            int b = 12; 
            int c; 
            for (int i = 2; i >= -2; i--) { 
                c = b / i;
                System.out.println("i=" + i); 
            } 
            return ret; 
        } catch (Exception e) { 
            System.out.println("testEx2, catch exception"); 
            ret = false; 
            throw e;
        } finally { 
            System.out.println("testEx2, finally; return value=" + ret); 
            //return ret;  //此处不写return 语句
        }
        //System.out.println("fhsdfsdofi");//因为try中有一个直接return语句,所以这两句不能被访问
        //return ret;
    } */
 
    //第四种:
     
    /*boolean testEx2() throws Exception{ 
        boolean ret = true; 
        try { 
 
            int b = 12; 
            int c; 
            for (int i = 2; i >= -2; i--) { 
                c = b / i;
                System.out.println("i=" + i); 
            }  
        } catch (Exception e) { 
            System.out.println("testEx2, catch exception"); 
            //ret = false; 
            throw e;
        } finally { 
            System.out.println("testEx2, finally; return value=" + ret); 
            //return ret;  //此处不写return 语句
        }
        System.out.println("这句话打印出来了!!!!");
        return ret;
    } */
    //第五种:
     
    /*boolean testEx2() throws Exception{  //提醒一下,第四种和第五种只有catch中有没有 throw e 不一样
        boolean ret = true; 
        try { 
 
            int b = 12; 
            int c; 
            for (int i = 2; i >= -2; i--) { 
                c = b / i;
                System.out.println("i=" + i); 
            } 
            //return ret; 
        } catch (Exception e) { 
            System.out.println("testEx2, catch exception"); 
            ret = false; 
            //throw e;
        } finally { 
            System.out.println("testEx2, finally; return value=" + ret); 
            //return ret;  //此处不写return 语句
        }
        System.out.println("这句话打印出来了!!!!!!!");
        return ret;
    }*/
     
    public static void main(String[] args) { 
        ExceptionTest testException1 = new ExceptionTest(); 
        try { 
            testException1.testEx(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
} 
 
class myException extends Exception{
   public void printException(){
      System.out.println("产生异常!");
   }
}
 
/*异常看着容易,理解起来也很容易!但是它真的没有你想像的那么简单!
第一种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
 
第二种:
i=2
i=1
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false
 
第三种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false
 
第四种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=true
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false
 
第五种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
这句话打印出来了!!!!!!!
testEx1, finally; return value=false
testEx, finally; return value=false
 
 
总结一下:
一:throw new Exception()
    第一种情形:(由第二种情形验证)
    int test() throws Exception{
       if(....)
          throw new Exception();
       ....
       return x;
    }
    第二中情形:第五种情况可验证
    int test() throws Exception{
       try{
         if(...)
           throw new Exception();
       }catch(ArithmeticException e){//在try中产生的异常没有被捕获时:
          ....
       }finally{
          ....
       }
       .....
       return x;
    }
   在执行到throw 时,第一种先将return返回个调用者(也就是throw和return之间的语句不会被执行),然后再调用程序中寻找处理异常的程序
   第二种还要将 finally中的语句执行完(即异常有没有被立即处理都要执行finally),(throw和return之间的语句也不会被执行),
   然后执行return语句,程序转向调用者中寻找异常处理程序。
二:再让我们看一下finally中写return的一些缺憾
1 finally块中的return语句会覆盖try块、catch块中的return语句
2 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,
而是会得到finally块的返回值,并且不会捕获异常,也就是如果在catch或者try中产生的异常如果在向外界抛出是不可能的。。。。
 
第一种情况:testEx2()方法中会产生一个ArithmeticException的异常, Exception是它的父类,我们在该方法中捕获了该异常并进行了处理
所以会输出 testEx2()中的 catch 和 finally 中的输出数据, 而在testEx1()方法中,调用了testEx2(),由于testEx2()中的异常已经被处理
并且由于finally中的return语句导致testEx2()中catch中的throw e 无法重新抛出,所以在testEx1()中不会被捕获。
 
再说一下第四,第五种情况:fianlly中都没有return
在第四种情况中, 由于在catch中继续抛出了该异常,该异常在testEx2()中没有被处理,所以在执行finally之后的语句(除了return语句)不会被执行,
而第五种是没有继续抛出该异常,也就是textEx2()中产生的异常全部被处理了,所以finally之后的语句会被执行.....其他不多说。
 
如果还是没有搞懂的话,推荐看一下下面这个链接,写的不错....
http://blog.csdn.net/hguisu/article/details/6155636
*/

目录
相关文章
|
5天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
305 116
|
20天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
503 45
Meta SAM3开源:让图像分割,听懂你的话
|
14天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
695 222
|
2天前
|
Windows
dll错误修复 ,可指定下载dll,regsvr32等
dll错误修复 ,可指定下载dll,regsvr32等
135 95
|
12天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1711 158
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
953 62