首页 > 代码库 > java中异常处理
java中异常处理
在做循环时,如果不做任何异常处理的话,一旦发生错误,程序将会停止,不会继续执行,这不会是我们想看到的结果。因此在加上try-catch处理后,程序遇到错误,会把错误抛出来,继续执行下一个循环。
这个是循环中没有处理异常
for (TalkBean talkBean : list) { String talk_mjbh=talkBean.getTalk_mjbh(); String talk_zfbh=talkBean.getTalk_zfbh(); String audiofile=talkBean.getAudiofile(); String remark=talkBean.getRemark(); String talk_address=talkBean.getTalk_address(); String talk_sub_type=talkBean.getTalk_sub_type(); String talk_thpj=talkBean.getTalk_thpj(); String talk_type=talkBean.getTalk_type(); System.out.println("文件名称"+audiofile); Dto rowDto_f1 = Dtos.newDto(); rowDto_f1.put("filename", audiofile); int countInt_f1 = uploadmobilefileinfoMapper.rows(rowDto_f1); if (countInt_f1 == 1) { Code = "103"; Codevalue = "重复上传录音文件。"; } Dto rowDto_p = Dtos.newDto(); rowDto_p.put("biz_code_", talk_mjbh); rowDto_p.put("delete_flag_", "0"); int countInt_p = aos_sys_userMapper.rows(rowDto_p); if (countInt_p == 0) { talk_mjbh="root"; } Dto rowDto_c = Dtos.newDto(); rowDto_c.put("zf_bh", talk_zfbh); rowDto_c.put("delete_flag_", "0"); int countInt_c = xw_base_prisonerMapper.rows(rowDto_c); Xw_th_talkeduPO xw_th_talkeduPO = new Xw_th_talkeduPO(); xw_th_talkeduPO.setId_(AOSId.uuid()); xw_th_talkeduPO.setAudit_remark(remark); Dto qDto_p = Dtos.newDto(); qDto_p.put("biz_code_", talk_mjbh); Dto police_Dto = sqlDao.selectDto("Talk.getPoliceId", qDto_p); String police_id = police_Dto.getString("id_"); String org_id_ = police_Dto.getString("org_id_"); Dto rowDto_c1 = Dtos.newDto(); rowDto_c1.put("zf_bh", talk_zfbh); rowDto_c1.put("org_id_", org_id_); rowDto_c1.put("delete_flag_", "0"); int countInt_c1 = xw_base_prisonerMapper.rows(rowDto_c1); if(countInt_c==0||countInt_c1 == 0){ uploadstate="false"; if (countInt_c1 == 0) { //如果不在一个监区,就把罪犯id设置为上传时读取到的罪犯编号 xw_th_talkeduPO.setPrisoner_id(talk_zfbh); } } xw_th_talkeduPO.setCreator(police_id); xw_th_talkeduPO.setCreattime(AOSUtils.getDateTimeStr()); xw_th_talkeduPO.setRemark(remark); xw_th_talkeduPO.setTalk_address(talk_address); xw_th_talkeduPO.setTalk_sub_type(talk_sub_type); xw_th_talkeduPO.setTalk_type(talk_type); xw_th_talkeduPO.setStatus("2"); xw_th_talkeduPO.setTalk_effect(talk_thpj); Aos_sys_orgPO aos_sys_orgPO = aos_sys_orgMapper.selectByKey(org_id_); xw_th_talkeduPO.setOrg_id_(org_id_); xw_th_talkeduPO.setOrg_cascade_id_(aos_sys_orgPO.getCascade_id_()); xw_th_talkeduPO.setUploadstate(uploadstate); xw_th_talkeduPO.setPolice_id(police_id); if (countInt_c == 1) { Dto qDto = Dtos.newDto(); qDto.put("zf_bh", talk_zfbh); String prisoner_id = (String) sqlDao.selectOne("Talk.getPrisonerId", qDto); System.out.println("prisoner_id是:"+prisoner_id); xw_th_talkeduPO.setPrisoner_id(prisoner_id); } String dateFormat = new SimpleDateFormat("yyyy年MM月dd日").format(new Date()); xw_th_talkeduPO.setTalk_date(dateFormat); xw_th_talkeduPO.setAudiofile(audiofile); xw_th_talkeduPO.setTime_length("0"); xw_th_talkeduPO.setIsfrommobile("1"); xw_th_talkeduPO.setTalk_means("1"); xw_th_talkeduMapper.insert(xw_th_talkeduPO); UploadmobilefileinfoPO uploadmobilefileinfoPO = new UploadmobilefileinfoPO(); uploadmobilefileinfoPO.setId_(AOSId.uuid()); uploadmobilefileinfoPO.setFilename(audiofile); uploadmobilefileinfoPO.setTime_(AOSUtils.getDateTimeStr()); uploadmobilefileinfoMapper.insert(uploadmobilefileinfoPO); Code = "success"; Codevalue = "上传文件成功。"; }
下面的是处理了异常,程序会继续往下执行
for (TalkBean talkBean : list) { try { String talk_mjbh=talkBean.getTalk_mjbh(); String talk_zfbh=talkBean.getTalk_zfbh(); String audiofile=talkBean.getAudiofile(); String remark=talkBean.getRemark(); String talk_address=talkBean.getTalk_address(); String talk_sub_type=talkBean.getTalk_sub_type(); String talk_thpj=talkBean.getTalk_thpj(); String talk_type=talkBean.getTalk_type(); System.out.println("文件名称"+audiofile); Dto rowDto_f1 = Dtos.newDto(); rowDto_f1.put("filename", audiofile); int countInt_f1 = uploadmobilefileinfoMapper.rows(rowDto_f1); if (countInt_f1 == 1) { Code = "103"; Codevalue = "重复上传录音文件。"; } Dto rowDto_p = Dtos.newDto(); rowDto_p.put("biz_code_", talk_mjbh); rowDto_p.put("delete_flag_", "0"); int countInt_p = aos_sys_userMapper.rows(rowDto_p); if (countInt_p == 0) { talk_mjbh="root"; } Dto rowDto_c = Dtos.newDto(); rowDto_c.put("zf_bh", talk_zfbh); rowDto_c.put("delete_flag_", "0"); int countInt_c = xw_base_prisonerMapper.rows(rowDto_c); Xw_th_talkeduPO xw_th_talkeduPO = new Xw_th_talkeduPO(); xw_th_talkeduPO.setId_(AOSId.uuid()); xw_th_talkeduPO.setAudit_remark(remark); Dto qDto_p = Dtos.newDto(); qDto_p.put("biz_code_", talk_mjbh); Dto police_Dto = sqlDao.selectDto("Talk.getPoliceId", qDto_p); String police_id = police_Dto.getString("id_"); String org_id_ = police_Dto.getString("org_id_"); Dto rowDto_c1 = Dtos.newDto(); rowDto_c1.put("zf_bh", talk_zfbh); rowDto_c1.put("org_id_", org_id_); rowDto_c1.put("delete_flag_", "0"); int countInt_c1 = xw_base_prisonerMapper.rows(rowDto_c1); if(countInt_c==0||countInt_c1 == 0){ uploadstate="false"; if (countInt_c1 == 0) { //如果不在一个监区,就把罪犯id设置为上传时读取到的罪犯编号 xw_th_talkeduPO.setPrisoner_id(talk_zfbh); } } xw_th_talkeduPO.setCreator(police_id); xw_th_talkeduPO.setCreattime(AOSUtils.getDateTimeStr()); xw_th_talkeduPO.setRemark(remark); xw_th_talkeduPO.setTalk_address(talk_address); xw_th_talkeduPO.setTalk_sub_type(talk_sub_type); xw_th_talkeduPO.setTalk_type(talk_type); xw_th_talkeduPO.setStatus("2"); xw_th_talkeduPO.setTalk_effect(talk_thpj); Aos_sys_orgPO aos_sys_orgPO = aos_sys_orgMapper.selectByKey(org_id_); xw_th_talkeduPO.setOrg_id_(org_id_); xw_th_talkeduPO.setOrg_cascade_id_(aos_sys_orgPO.getCascade_id_()); xw_th_talkeduPO.setUploadstate(uploadstate); xw_th_talkeduPO.setPolice_id(police_id); if (countInt_c == 1) { Dto qDto = Dtos.newDto(); qDto.put("zf_bh", talk_zfbh); String prisoner_id = (String) sqlDao.selectOne("Talk.getPrisonerId", qDto); System.out.println("prisoner_id是:"+prisoner_id); xw_th_talkeduPO.setPrisoner_id(prisoner_id); } String dateFormat = new SimpleDateFormat("yyyy年MM月dd日").format(new Date()); xw_th_talkeduPO.setTalk_date(dateFormat); xw_th_talkeduPO.setAudiofile(audiofile); xw_th_talkeduPO.setTime_length("0"); xw_th_talkeduPO.setIsfrommobile("1"); xw_th_talkeduPO.setTalk_means("1"); xw_th_talkeduMapper.insert(xw_th_talkeduPO); UploadmobilefileinfoPO uploadmobilefileinfoPO = new UploadmobilefileinfoPO(); uploadmobilefileinfoPO.setId_(AOSId.uuid()); uploadmobilefileinfoPO.setFilename(audiofile); uploadmobilefileinfoPO.setTime_(AOSUtils.getDateTimeStr()); uploadmobilefileinfoMapper.insert(uploadmobilefileinfoPO); Code = "success"; Codevalue = "上传文件成功。"; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
记住,在可能出错的代码外面,包裹一层try-catch。
本文出自 “YuanGuShi” 博客,请务必保留此出处http://cm0425.blog.51cto.com/10819451/1934804
java中异常处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。