Date date = null; try {
date = em1format.parse(sDisplay);
} catch (ParseException e) {
LogUtil.error(AAConstants.PACKAGE_NAME, e.toString(), e);
} if (date != null) {
sDisplay = format.format(date);
tmpVector.add(new TextElement(sDisplay));
} else {
tmpVector.add(new TextElement());
}
想問的是:當sDisplay為非法日期格式拋出異常後,變量date有沒有可能是null以外的其它值?

解决方案 »

  1.   

    没有可能,对于 date = em1format.parse(sDisplay);语句
    em1format.parse(sDisplay);先执行,抛出异常的话date = 这个赋值操作自然没有执行,实际上也没有值可以赋
    ====================================================
    你自己给sDisplay一个不合法的值,测试一下不就ok了
      

  2.   

    对于 date = em1format.parse(sDisplay);
    当sDisplay為非法日期格式拋出异常后,
    只会执行以下这句
    LogUtil.error(AAConstants.PACKAGE_NAME, e.toString(), e);
    date 仍为null,不可能为其它值
      

  3.   

    If the method throws exception during the operation, the return value is indeterminate because we do not know how JVM handles abnormal situation. I am concerned that JVM does not handle abnormal flow intelligently and corrupt the memory address date variable references to.這是香港同事給我的回復,各位朋友覺他說的有沒道理?
      

  4.   


    他担心jvm内部是如何处理这种情况,也可能不同的jvm会处理不同, 究竟是不是所有jvm都一样,我想sun能回答你. 如果你不确定建议写如下代码
    try {
            date = em1format.parse(sDisplay);
        } catch (ParseException e) {
            date = null //加上这句,无论jvm怎样处理,你都不必担心, 我一般也是加上这句的.
            LogUtil.error(AAConstants.PACKAGE_NAME, e.toString(), e);
        }
      

  5.   

    你要确定的答案最好问sun, 如果有人告诉你完全没问题,你信不信? 现在的jvm可以,谁敢说以后的jvm是否一样,所以只有sun能告诉你.
      

  6.   

    你要确定的答案最好问sun, 如果有人告诉你完全没问题,你信不信? 最多只能确定现在的jvm版本中你的代码没问题.
      

  7.   

    应该不过能的,出现异常相当于代码中止执行了,初始值为null,之后还是null
      

  8.   

        Date date = null;    try {
            date = em1format.parse(sDisplay);
        } catch (ParseException e) {
               这里写你要改的的代码
                  eg:date=null;       
     
        }    if (date != null) {
            sDisplay = format.format(date);
            tmpVector.add(new TextElement(sDisplay));
        } else {
            tmpVector.add(new TextElement());
        }
      

  9.   

    问sun也不行啊,
    jvm又不是只有sun出。
      

  10.   

    如果楼主你怕 不是 null 就直接赋值 null 咯 ~
      

  11.   

    抛出异常后是对date不处理的。所以date在这里应该是null.............