public static String changeF2Y(String amount) throws Exception {
amount = amount.trim();
if (!amount.matches(CURRENCY_FEN_REGEX)) {
throw new Exception("金额格式有误");
}
amount = BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();
if(!amount.contains(".")){
amount = amount + ".00";
}
return amount;
}
如何将以下throw 语句更改为try catch?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
关注去掉throws,直接将可能出现异常的地方放置try-catch中。
示例代码:public static String changeF2Y(String amount) { try{ amount = amount.trim(); if (!amount.matches(CURRENCY_FEN_REGEX)) { throw new Exception("金额格式有误"); } amount = BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString(); if(!amount.contains(".")){ amount = amount + ".00"; } }catch(Exception e){ e.printStackTrace(); } return amount; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报