Below is the class somebody else wrote.
The problem that I am facing is that when it get’s into the parse method with null as the rawString, it is throwing NumberFormatException.
So what I was thinking to do is, I should catch that NumberFormatException and set the value itself as null. So the way I did is right?
public class ByteAttr {
@JExType(sequence = 1)
private Byte value;
public static ByteAttr parse(String rawString) {
ByteAttr attr = new ByteAttr();
try {
attr.setValue(Byte.valueOf(rawString));
} catch (NumberFormatException nfEx) {
attr.setValue(null);
}
return attr;
}
public Byte getValue() {
return this.value;
}
public void setValue(Byte value) {
this.value = value;
}
}
The correct approach depends on what you want to accomplish in the program.
ByteAttr.getValue()to returnnulllater in your program, then your approach could work.parseis called with an indecipherable argument (includingnull). An alternative is to catch theNumberFormatExceptionand throw a different exception that has semantic meaning in your program.public static ByteAttr parse(String rawString) throws BadAttributeException { ByteAttr attr = new ByteAttr(); try { attr.setValue(Byte.valueOf(rawString)); } catch (NumberFormatException nfEx) { throw new BadAttributeException(nfEx); // wrap original exception } return attr; }parsefor those cases whenrawStringis indecipherable:public static ByteAttr parse(String rawString, Byte defaultValue) { ByteAttr attr = new ByteAttr(); try { attr.setValue(Byte.valueOf(rawString)); } catch (NumberFormatException nfEx) { attr.setValue(default); } return attr; }