说我有这个方法:
public long getLongId(JWTClaimsSet claimsSet)
{
return Long.parseLong(String.valueOf(claimsSet.getClaim(LONG_ID_CLAIM)));
}
public int getIntId(JWTClaimsSet claimsSet)
{
return Integer.parseInt(String.valueOf(claimsSet.getClaim(ID_CLAIM)));
}
对于这两个方法,如果我不发送有效的int或long,我希望该方法返回默认值0。我是否需要在此处捕获NumberFormatException或在内部进行处理?
如果不确定是什么类型的数据类型,则来自JWTClaimsSet ClaimsSet:
public long getLongId(JWTClaimsSet claimsSet) {
try {
return Long.parseLong(String.valueOf(claimsSet.getClaim(LONG_ID_CLAIM)));
} catch (NumberFormatException e) {
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public int getIntId(JWTClaimsSet claimsSet) {
try {
return Integer.parseInt(String.valueOf(claimsSet.getClaim(ID_CLAIM)));
} catch (NumberFormatException e) {
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}