Exception Handling in Java

Source: Internet
Author: User
Tags dateformat

In the loop, if you do not do any exception handling, if an error occurs, the program will stop, will not continue to execute, this is not the result we want to see. So after adding try-catch processing, the program encounters an error, throws the error, and proceeds to the next loop.

This is a loop that doesn't handle exceptions.

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 ("file name" +audiofile);D to rowdto_f1 = dtos.newdto (); Rowdto_f1.put ("FileName",  audiofile); Int countint_f1 = uploadmobilefileinfomapper.rows (rowDto_f1);if  (countInt _f1 == 1)  {Code =  "103"; codevalue =  "Repeat to upload the recording file. ";} 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);D to qdto_p = dtos.newdto (); Qdto_p.put ("Biz_code_",  TALK_MJBH);D to police_dto =   sqldao.selectdto ("Talk.getpoliceid",  qDto_p) ; String police_id = police_dto.getstring ("Id_"); String org_id_ = police_dto.getstring ("Org_id_");D to 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)  { //if not in a prison, Just set the criminal ID.The offender number xw_th_talkedupo.setprisoner_id (TALK_ZFBH) that was read at the time of uploading;}} 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);    &nBsp System.out.println ("prisoner_id is:" +prisoner_id);    xw_th_talkedupo.setprisoner_id (prisoner_id);    }string dateformat = new simpledateformat ("yyyy mm month DD Day"). 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 =  "Upload file successfully. ";}

The following exception is handled, and the program continues to execute down

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 ("file name" +audiofile);D to rowdto_f1 = dtos.newdto (); Rowdto_f1.put ("FileName",  audiofile); Int countint_f1 = uploadmobilefileinfomapper.rows (rowDto_f1);if  (countInt _f1 == 1)  {Code =  "103"; codevalue =  "Repeat to upload the recording file. ";} 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&nBsp;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);D to qdto_p = dtos.newdto (); Qdto_p.put ("Biz_code_",  TALK_MJBH);D to police_dto =   sqldao.selectdto ("Talk.getpoliceid",  qDto_p) ; String police_id = police_dto.getstring ("Id_"); String org_id_ = police_dto.getstring ("Org_id_");D to 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)  { //if not a, the offender ID is set to the offender number xw_th_talkedupo.setprisoner_id (TALK_ZFBH) that was read at the time of the upload;}} 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 is:" +prisoner_id); xw_th_talkEDUPO.SETPRISONER_ID (prisoner_id);} String dateformat = new simpledateformat ("yyyy mm month DD Day"). 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 =  "Upload file successfully. ";}  catch  (exception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}


Remember to wrap a layer of try-catch outside the code that might be wrong.

This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1934804

Exception Handling in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.