Java 从异常获取值
我得到一个NumberFormatException,我想从异常本身获取值,它看起来像Java 从异常获取值,java,exception,numberformatexception,downcast,Java,Exception,Numberformatexception,Downcast,我得到一个NumberFormatException,我想从异常本身获取值,它看起来像 但是该值没有可用的getter,是否有人可以建议任何解决方案来获取该值而不是反射,或者反射也可以。如果显式检查,只需调用getValue() 您可以从org.springframework.beans.typemischException获取值,例如,只需使用Object getValue(),使用以下代码: ... } catch(Exception exception) { if(exceptio
但是该值没有可用的getter,是否有人可以建议任何解决方案来获取该值而不是反射,或者反射也可以。如果显式检查,只需调用
getValue()
您可以从org.springframework.beans.typemischException获取值,例如,只需使用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
}
因为定义为
package org.springframework.beans;
public class TypeMismatchException extends PropertyAccessException {
...
/**
* Return the offending value (may be {@code null})
*/
@Override
public Object getValue() {
return this.value;
}
...
}
扩展异常并添加您自己的值/getter。实际上,你需要它做什么?@Stultuske自己的异常在这里应该有什么帮助?使用过的库中没有一个会知道这个新的异常类型。通过获取这个值,您试图实现什么目的?@Stultuske:谢谢您的建议,但是当spring尝试使用jackson@KulbhushanSingh:我必须使用此值进行日志记录。很抱歉,这是意外发生的,连我都不知道!!!我又接受了,谢谢