try catch throw在编程里面的应用

我们可以在try里面写代码,在一些异常情况下我们throw new Exception("不存在此挂号预约");

这样在catch (Exception e)里面我们就可以获取异常信息并作相应的返回信号

public Map<String, Object> PUTGHYSQX(Map<String, Object> reqMap) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap.put("code", "0");
            resultMap.put("msg", "SUCCESS");
            String shxh = CommUtil.getStringVal(reqMap, "shxh");
            if (CommUtil.checkNull(shxh)) {
                throw new Exception("锁号序号shxh不能为空!");
            }
            String patid = CommUtil.getStringValNull(reqMap, "patid");
            if (CommUtil.checkNull(patid)) {
                throw new Exception("门诊patid不能为空!");
            }
            List<Map> ghyy = queryGHYY(new HashMap() {
                {
                    put("ORDERID", shxh);
                }
            });
            if (ghyy.size() == 0) {
                throw new Exception("不存在此挂号预约");
            }    
            if (CommUtil.getIntVal(ghyy.get(0), "F_ZFZT") == 1) {
                throw new Exception("此挂号预约已支付,不能取消");
            }
            if (CommUtil.getIntVal(ghyy.get(0), "F_HasGH") == 1) {
                throw new Exception("此挂号预约已挂号,不能取消");
            }
            if (CommUtil.getIntVal(ghyy.get(0), "F_ISTY") == 1) {
                throw new Exception("此挂号预约已退约");
            }
            updateTable(Constants.MZ_GHYY, new HashMap(){
                {
                    put("ORDERID", shxh);
                }
            }, new HashMap(){
                {
                    put("F_ISTY", 1);
                }
            });
            subYSSYHYHZ(ghyy.get(0).get("KSBM").toString(),
                    ghyy.get(0).get("YS").toString(),
                    Integer.parseInt(ghyy.get(0).get("RQ").toString()),
                    Integer.parseInt(ghyy.get(0).get("APM").toString()));
            subYSSYHYMX(ghyy.get(0).get("KSBM").toString(),
                    ghyy.get(0).get("YS").toString(),
                    Integer.parseInt(ghyy.get(0).get("RQ").toString()),
                    Integer.parseInt(ghyy.get(0).get("APM").toString()),
                    ghyy.get(0).get("SJD").toString());
        } catch (Exception e) {
            resultMap.put("code", "-1");
            resultMap.put("msg", "FAIL");
            resultMap.put("result", e.getMessage());
            getLogger().error(e.getMessage());
        }
        return resultMap;
    }
原文地址:https://www.cnblogs.com/luzhanshi/p/13167710.html