第四章Java异常处理1、写程序运行结果:publicclassA{staticintsome(){try{System.out.println(try);return1;}finally{System.out.println(finally);}}publicstaticvoidmain(Stringarg[]){System.out.println(some());}}结果:tryfinally12、先从键盘输入一个十六进制数,再将其转化为十进制数,然后输出。若输入的不是一个有效的十六进制数,则抛出异常。importjava.util.Scanner;classExchange{Strings;privateinty;Exchange(){}Exchange(Strings){this.s=s;}intgetY(){returny;}voidsetY(inty){this.y=y;}voidChange16To10(){inty=0;inti,k;for(i=0;is.length();i++){k=Change(s.charAt(i));y+=k*Math.pow(16,s.length()-(i+1));}System.out.println(y);System.out.println(成功转换);}intChange(charh){intx;switch(h){case'0':x=0;break;case'1':x=1;break;case'2':x=2;break;case'3':x=3;break;case'4':x=4;break;case'5':x=5;break;case'6':x=6;break;case'7':x=7;break;case'8':x=8;break;case'9':x=9;break;case'a':x=10;break;case'b':x=11;break;case'c':x=12;break;case'd':x=13;break;case'e':x=14;break;case'f':x=15;break;default:thrownewMyException();}returnx;}}classMyExceptionextendsRuntimeException{publicMyException(){}publicMyException(Stringmsg){super(msg);}publicvoidprintcount(){System.out.println(16进制数输入不正确!);}}publicclassTestExchange{publicstaticvoidmain(String[]args){System.out.println(请输入一个16进制数:);ScannerS=newScanner(System.in);Strings=S.next();ExchangeE=newExchange(s);try{E.Change16To10();}catch(MyExceptione){e.printcount();}}}3、先编写一个方法,它将格式为“yyyy/mm/dd”形式的日期字符串转化为日期对象。若日期字符串不符合以上规定,则抛出异常。再在main方法中对正常和异常输入的日期字符串分别进行验证,并输出转换后的日期对象。importjava.util.Date;publicclassDate{publicstaticvoidmain(String[]args){System.out.println(当前时刻为:);System.out.println(newDate().toString());System.out.println(转化显示格式后:);System.out.println(convertCommonDateStr(newDate().toString()));}publicstaticStringconvertCommonDateStr(StringcommonDateStr){StringBufferresult=newStringBuffer();String[]arrStr=commonDateStr.split();result.append(arrStr[5]).append(-);result.append(convertMonthStr(arrStr[1],true)).append(-);result.append(arrStr[2]).append();result.append(arrStr[3]);returnresult.toString();}publicstaticStringconvertMonthStr(StringmonthStr,booleanisWithZero){StringmonthResult=;if(Jan.equals(monthStr))monthResult=01;elseif(Feb.equals(monthStr))monthResult=02;elseif(Mar.equals(monthStr))monthResult=03;elseif(Apr.equals(monthStr))monthResult=04;elseif(May.equals(monthStr))monthResult=05;elseif(Jun.equals(monthStr))monthResult=06;elseif(Jul.equals(monthStr))monthResult=07;elseif(Aug.equals(monthStr))monthResult=08;elseif(Sep.equals(monthStr))monthResult=09;elseif(Oct.equals(monthStr))monthResult=10;elseif(Nov.equals(monthStr))monthResult=11;elseif(Dec.equals(monthStr))monthResult=12;if(!isWithZero){if(monthResult.startsWith(0))monthResult=monthResult.substring(1,2);}returnmonthResult;}}