wallyone 2015-04-15 03:19 采纳率: 25%
浏览 1673
已采纳

如何将以下throw 语句更改为try catch?

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;
}
  • 写回答

3条回答 默认 最新

  • 毕小宝 领域专家: 后端开发技术领域 2015-04-15 09:24
    关注

    去掉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;
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?