资源描述:
《java基础 异常及常见包》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Part11异常及常见包u了解常见的异常解决异常的方式:()用trycatch和finally语句语句结构:如下:try{//需要排除异常的代码}catch(Exceptione){//Exceptione异常处理代码}finally{//程序结束前的执行的代码}案例应用:源代码如下:packagecom.workit.part1;publicclassDemo1{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubDemo1div=newDemo1(
2、);try{inttemp=div.Div(10,0);otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbee
3、ntheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,MinistersandSystem.out.println("temp="+temp);}catch(ArithmeticExceptione){System.out.println("除数不能为零!输入错误!!!
4、");System.out.println(e.toString());//输出错误处}finally{System.out.println("=========程序结束=========");}}publicintDiv(intfirst,intsecond){intresult=0;result=first/second;returnresult;}}多重try——catch的应用(根类在前时就捕获不到在后子类的错误)案例应用:源代码如下:packagecom.workit.part1;publicclassDemo2{/***@paramargs*/
5、publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubDemo2data=newDemo2();String[]str={"10","t"};data.test(str);}publicvoidtest(String[]str){try{try{System.out.println("data2="+Integer.parseInt(str[1]));}catch(ArrayIndexOutOfBoundsExceptione){System.out.println("数组溢出,请重新
6、输入!!!");e.printStackTrace();}catch(NumberFormatExceptione){//TODOAuto-generatedcatchblockSystem.out.println("输入错误!非字符型字符串无法转化成整型!!!"+""+e.toString());otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.S
7、tartingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCom
8、mitteeSecretary,Min