六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 49|回复: 0

有关java的异常处理

[复制链接]

升级  20%

2

主题

2

主题

2

主题

童生

Rank: 1

积分
10
 楼主| 发表于 2013-1-27 05:20:04 | 显示全部楼层 |阅读模式
看下下面这段代码:
public class TestException {    public TestException() {    }    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() throws Exception{        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 true;        }catch (Exception e){            System.out.println("testEx2, catch exception");            ret = false;            throw e;        }        finally{            System.out.println("testEx2, finally; return value="+ret);            return ret;        }    }    public static void main(String[] args) {        TestException testException1 = new TestException();        try{            testException1.testEx();        }catch(Exception e){            e.printStackTrace();        }    }}
结果是什么?

注意java中能改变控制权的语句,比如return,break,continue,throw等。写代码的时候一定要注意finally里面的return语句,否则可能导致上层程序无法捕获内部方法抛出的异常。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表