下面是代码片段。
public class Operand<T> {
private OperandStore store;
private final int operandType, operandId;
public Operand( int operandType, int operandId, OperandStore store ) {
this.operandId = operandId;
this.operandType = operandType;
this.store = store;
}
@SuppressWarnings( "unchecked" )
public T evaluate() {
try {
return ( T ) store.getValue( operandType, operandId );
}
catch ( Exception e ) {
return null;
}
}
}
我的getValue
方法:
public Object getValue(int operandType, int operandId ) {
// return some value from a <String, Object> hashmap based on some condition
}
当我创建上述类的对象时,如下所示:
Operand<Integer> obj = new Operand<>(1,1,store);
...并确保store.getValue( operandType, operandId )
返回一个字符串,我希望try
块中会发生错误,而该错误不会发生。它正在返回string
值。
任何原因?请解释。谢谢。