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条)

报告相同问题?

悬赏问题

  • ¥15 overleaf中论文编辑,报错`pages' is a missing field, not a string, for entry 4
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么