子类向上抛异常问题

今天项目需求:友好化异常信息,不能给客户看到你的异常信息,什么nullpointException啊,客户看不懂的,你需要把它友好化为中文

protected abstract String SaveTempData(T entity,UserModel user) throws Exception;//抛出异常

    // 保存信息 --对日志信息表的操作
    public String saveExcelData(String xmlPath, MultipartFile multiFile, UserModel user, Map extendField,String hospitalid) {
        try {
            String filename = multiFile.getOriginalFilename();
            InputStream excelSteam = multiFile.getInputStream();
            List list = ExcelParserUtil.getExcelBean(xmlPath, excelSteam);
            if (list != null) {
                int len = list.size();
                T entity = null;
                for (int i = 0; i < len; i++) {
                    entity = (T) list.get(i);
                    //没有特殊处理
                    if (extendField != null) {
                        Set<String> keySet = extendField.keySet();
                        for (String key : keySet) {
                            this.setField(entity, key, extendField.get(key));
                        }
                    }
                    entity.setImportfrom(filename);
                    if(StringUtils.isNotBlank(hospitalid)){
                        entity.setHospitalid(hospitalid);
                    }
                    String mess =SaveTempData(entity,user);
                    if("succ".equals(mess)){
                        continue;
                    }else{
                        return "第"+(i+1)+"行"+mess;
                    }
                }
                list = null;
            }
        } catch (Exception e) {
            // 表空间满、主索引数据已存在
            e.printStackTrace();
        }
        return "succ";
    }

我这个是抽象方法里的异常,需要点进去调用,在这边说明一下,因为这个抽象方法有三种不同的类继承,所以你需要Ctrl点进去查看

下面就是比较重要的,之前可能是自己开发经验太少了,没想到,就是你不能所有的方法都向上抛出,因为我们很清楚有些异常捕获了不好向上抛出,那我们怎么办呢?项目经理提示了一下,我给大家展示一下

protected String SaveTempData(MedicalDoctorModel entity, UserModel user) throws Exception {
        //entity异常处理多个判断
        String idcard = entity.getIdcard();
        if (StringUtils.isBlank(entity.getIdcard())) {
            return "身份证为空";
        }
        MedicalpersonModel medicalpersonModel = medicalpersonDao.getByNumber(idcard);
            ExcelBaseModel excelBaseModel = new ExcelBaseModel();
            // 导入数据使用自动生成的ID
            String newid = UUIDUtil.generate();
            excelBaseModel.setId(newid);
            excelBaseModel.setPersonid(entity.getIdcard());
            excelBaseModel.setWhocreateid(user.getId());
            excelBaseModel.setImportfrom(entity.getImportfrom());
        if(medicalpersonModel != null){
            excelBaseModel.setImportname(medicalpersonModel.getUnitname());
        }

        Map map1 = (Map) CommonWebUtil.dicMaps.get("persontypeMap");
        if (entity.getApplypratype()!=null && !map1.containsKey(entity.getApplypratype())) {
            return "执业类别代码错误";
        }

        Map map2 = (Map) CommonWebUtil.dicMaps.get("embark_typeMap");
        if (entity.getApplypratype2()!=null && !map2.containsKey(entity.getApplypratype2())) {
            return "单位类别代码错误";
        }

        Map map3 = (Map) CommonWebUtil.dicMaps.get("nurse_jobtypeMap");
        if (entity.getJobtype()!=null && !map3.containsKey(entity.getJobtype())) {
            return "类别错误";
        }

         //1导入冲突;
         excelBaseModel.setImportresult("1");
        if(medicalpersonModel == null){
         //2针对子表的主表数据不存在或主表中已删除
         excelBaseModel.setImportresult("2");
        }
        try {
            excelImportDao.save(excelBaseModel);//之前抛出异常
            if(medicalpersonModel != null){
                String id = medicalpersonModel.getId();
                if(medicalDoctorDao.getById(id) == null){
                    entity.setWhocreateid(user.getId());
                    //向doctor中insert
                    entity.setId(id);
                    entity.setSourcetype("excel导入");
                    medicalDoctorDao.save(entity);
                    //在临时表中删除对应记录(物理删除)
                    excelImportDao.remove(excelBaseModel);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "succ";  
    }

看了上面的代码大家应该清楚了,空指针我们可以直接判断,然后return一条友好的String字符串出来,这样就不要catch抛出了,catch这个可以抛出一下表空间满了的后台错误,由管理员处理。这就是我这段编程的思想。

当然还要说一下这一节的重点,就是子类抛出异常,父类处理的问题,我就怕麻烦,直接贴出代码了,欢迎大家有问题和我交流:

public class ExceptionTest {

    public static void main(String[] args) {
        Person person1 = new Person();
        try {
            person1.run();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

class Person{
    public void run() throws InterruptedException{
        try{
            Thread.sleep(100);
        }catch (InterruptedException e) {
              throw new InterruptedException(); // 在此抛出的null异常可以不用在方法后面throws;且此异常出现一定会被捕获到;
         }
    }
}

这里面会设计到throw 和throws 的一些区别,这个暂时还未真理,以后有机会再添加,大家也可以百度一下,谢谢大家。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值