从Exception获取值

时间:2015-12-11 11:46:26

标签: java exception numberformatexception downcast

我得到一个NumberFormatException,我想从异常本身获取值,看起来像是 enter image description here

但是没有可用于该值的getter,任何人都可以建议任何解决方法来获取此值而不是反射,甚至反射也可以。

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:2)

你可以从org.springframework.beans.TypeMismatchException获取价值,只需使用Object getValue(),例如,使用以下代码:

...
} catch(Exception exception) {
   if(exception instanceof TypeMismatchException) {
      Object value = ((TypeMismatchException) exp).getValue;
      ... // what you want to do with value
   }
}

或只是

...
} catch(TypeMismatchException exception) {
      Object value = exp.getValue;
      ... // what you want to do with value
}

因为org.springframework.beans.TypeMismatchException定义为

package org.springframework.beans;
public class TypeMismatchException extends PropertyAccessException {
...
    /**
     * Return the offending value (may be {@code null})
     */
    @Override
    public Object getValue() {
        return this.value;
    }
...
}