A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小黑马 黑马帝   /  2012-9-18 10:53  /  1858 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static Object test1(Object obj,String propertyName){
  2.        
  3.                     try {
  4.                                  PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
  5.           Method met = pd.getReadMethod();
  6.                                  Object retValue = met.invoke(obj, propertyName);
  7.                                  return retValue;  //位置1
  8.                    } catch (Exception e) {
  9.                                  throw new RuntimeException(e);
  10.     }       
  11.                         //位置2
  12.         }
复制代码
代码1
  1. public static Object getProperty(Object obj,String propertyName)
  2.                     try {
  3.                                   PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
  4.                                   Method met = pd.getReadMethod();
  5.                                   Object retValue = met.invoke(obj, propertyName);
  6.                                    return retValue; //位置1
  7.                     } catch (Exception e) {
  8.                                 e.printStackTrace();
  9.                     }       
  10.                        return obj; //位置2
  11.         }
复制代码
代码2

问题一: 代码1 当catch语句中抛出 RuntimeException()这个异常后,位置2处不用写return语句,而代码2中,打印异常的话,能在位置1和位置2都能写返回值.
问题二: 代码2中, 如果写了两个return, 最后返回的是哪一个?
问题三: 代码1中, return 是写在 那个位置上好,为什么?

评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
本帖最后由 彭润生 于 2012-9-18 11:19 编辑

第一个 你抛出RuntimeException程序就停止了,所以后面可以没返回值。
第二个
public static Object getProperty(Object obj,String propertyName)

                    try {

                                  PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());

                                  Method met = pd.getReadMethod();

                                  Object retValue = met.invoke(obj, propertyName);//如果上面任何一个抛出异常,他下面的代码不执行了,直接catch捕获。

                                   return retValue; //位置1

                    } catch (Exception e) {

                                e.printStackTrace();

                    }        //捕获之后接着执行下面的return
                       return obj; //位置2

        }
第三个,两个位置写在那儿都行,你抛出的是Runtime有异常那么就程序停止,没有异常则返回值

评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

回复 使用道具 举报
问题一:一个两个都不影响他正常运行
问题二:第一种情况,如果运行try语句出现异常会跳转出去第一个return就无法运行,会运行第二个return,返回的是第二个return;
第二种情况,如果不出现异常第一个return运行就返回第一个return,这时候程序结束运行,第二个return就运行不到。
问题三:写在位置二上好,这样保证不管有没有,一定有返回值。
回复 使用道具 举报
public static Object test1(Object obj,String propertyName){
        
                    try {
                                 PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
          Method met = pd.getReadMethod();
                                 Object retValue = met.invoke(obj, propertyName);
                                 return retValue;  //位置1
                   } catch (Exception e) {
                                 throw new RuntimeException(e);
    }        
                        //位置2
        }

代码1

public static Object getProperty(Object obj,String propertyName)
                    try {
                                  PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
                                  Method met = pd.getReadMethod();
                                  Object retValue = met.invoke(obj, propertyName);
                                   return retValue; //位置1
                    } catch (Exception e) {
                                e.printStackTrace();
                    }        
                       return obj; //位置2
        }

代码2

问题一: 代码1 当catch语句中抛出 RuntimeException()这个异常后,位置2处不用写return语句,而代码2中,打印异常的话,能在位置1和位置2都能写返回值.
答:代码1中的RuntimeException()是运行时异常,一但抛出该异常,程序就会结束,那么//位置2中的语句此时是执行不到的,除非将它放到finally中。
       代码2中因为抛的是 e.printStackTrace();打印异常,程序不会停止,所以会继续执行return obj; //位置2语句,而try如果异常,直接捕获,不会执行return retValue; //位置1。所以位置1
       和位置
2都能写返回值。

问题二: 代码2中, 如果写了两个return, 最后返回的是哪一个?
答:代码2中,如果没有异常发生,return retValue; //位置1,程序结束。如果发生异常,捕获打印异常后,执行return obj; //位置2

问题三: 代码1中, return 是写在 那个位置上好,为什么?
答:代码1中,如果是return retValue; 那么就只能写在//位置1中,因为retValue是try中的局部变量。而且一但发生异常,//位置2是执行不到的。

评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马