电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx

上传人:b****3 文档编号:6428155 上传时间:2023-05-06 格式:DOCX 页数:12 大小:23.88KB
下载 相关 举报
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第1页
第1页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第2页
第2页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第3页
第3页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第4页
第4页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第5页
第5页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第6页
第6页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第7页
第7页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第8页
第8页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第9页
第9页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第10页
第10页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第11页
第11页 / 共12页
电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx

《电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx》由会员分享,可在线阅读,更多相关《电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx(12页珍藏版)》请在冰点文库上搜索。

电子商务毕业论文外文翻译范文《Thinking In Java》Word下载.docx

surprisingly,theywouldn’tcheckfortheerrorconditions(andsometimestheerrorconditionsweretoosillytocheckfor).Ifyouwerethoroughenoughtocheckforanerroreverytimeyoucalledamethod,yourcodecouldturnintoanunreadablenightmare.Becauseprogrammerscouldstillcoaxsystemsoutoftheselanguages,theywereresistanttoadmittingthetruth:

thatthisapproachtohandlingerrorswasamajorlimitationtocreatinglarge,robust,maintainableprograms.

Thesolutionistotakethecasualnatureoutoferrorhandlingandtoenforceformality.Thisactuallyhasalonghistory,becauseimplementationsofexceptionhandlinggobacktooperatingsystemsinthe1960s,andeventoBASIC’s“onerrorgoto.”ButC++exceptionhandlingwasbasedonAda,andJava’sisbasedprimarilyonC++(althoughitlooksmorelikethatinObjectPascal).

Theword“exception”ismeantinthesenseof“Itakeexceptiontothat.”Atthepointwheretheproblemoccurs,youmightnotknowwhattodowithit,butyoudoknowthatyoucan’tjustcontinueonmerrily;

youmuststop,andsomebody,somewhere,mustfigureoutwhattodo.Butyoudon’thaveenoughinformationinthecurrentcontexttofixtheproblem.Soyouhandtheproblemouttoahighercontextwheresomeoneisqualifiedtomaketheproperdecision(muchlikeachainofcommand).

Theotherrathersignificantbenefitofexceptionsisthattheycleanup

1

errorhandlingcode.Insteadofcheckingforaparticularerroranddealingwithitatmultipleplacesinyourprogram,younolongerneedtocheckatthepointofthemethodcall(sincetheexceptionwillguaranteethatsomeonecatchesit).And,youneedtohandletheprobleminonlyoneplace,theso-calledexceptionhandler.Thissavesyoucode,anditseparatesthecodethatdescribeswhatyouwanttodofromthecodethatisexecutedwhenthingsgoawry.Ingeneral,reading,writing,anddebuggingcodebecomesmuchclearerwithexceptionsthanwhenusingtheoldwayoferrorhandling.

BecauseexceptionhandlingistheonlyofficialwaythatJavareportserrors,anditisenforcedbytheJavacompiler,thereareonlysomanyexamplesthatcanbewritteninthisbookwithoutlearningaboutexceptionhandling.Thischapterintroducesyoutothecodeyouneedtowritetoproperlyhandleexceptions,andthewayyoucangenerateyourownexceptionsifoneofyourmethodsgetsintotrouble.

1.Basicexceptions

Anexceptionalconditionisaproblemthatpreventsthecontinuationofthemethodorscopethatyou’rein.It’simportanttodistinguishanexceptionalconditionfromanormalproblem,inwhichyouhaveenoughinformationinthecurrentcontexttosomehowcopewiththedifficulty.Withanexceptionalcondition,youcannotcontinueprocessingbecauseyoudon’thavethe

informationnecessarytodealwiththeprobleminthecurrentcontext.Allyoucandoisjumpoutofthecurrentcontextandrelegatethatproblemtoahighercontext.Thisiswhathappenswhenyouthrowanexception.

Divisionisasimpleexample.Ifyou’reabouttodividebyzero,it’sworthcheckingforthatcondition.Butwhatdoesitmeanthatthedenominatoriszero?

Maybeyouknow,inthecontextoftheproblemyou’retryingtosolveinthat

particularmethod,howtodealwithazerodenominator.Butifit’sanunexpectedvalue,youcan’tdealwithitandsomustthrowanexceptionratherthancontinuingalongthatexecutionpath.

Whenyouthrowanexception,severalthingshappen.First,theexceptionobjectiscreatedinthesamewaythatanyJavaobjectiscreated:

ontheheap,withnew.Thenthecurrentpathofexecution(theoneyoucouldn’tcontinue)isstoppedandthereferencefortheexceptionobjectisejectedfromthecurrentcontext.Atthispointtheexceptionhandlingmechanismtakesoverandbeginstolookforanappropriateplacetocontinueexecutingtheprogram.This

2

appropriateplaceistheexceptionhandler,whosejobistorecoverfromtheproblemsotheprogramcaneithertryanothertackorjustcontinue.

Asasimpleexampleofthrowinganexception,consideranobjectreferencecalledt.It’spossiblethatyoumightbepassedareferencethathasn’tbeeninitialized,soyoumightwanttocheckbeforetryingtocallamethodusingthatobjectreference.Youcansendinformationabouttheerrorintoalargercontextbycreatinganobjectrepresentingyourinformationand“throwing”itoutofyourcurrentcontext.Thisiscalledthrowinganexception.

1.1Exceptionarguments

LikeanyobjectinJava,youalwayscreateexceptionsontheheapusingnew,whichallocatesstorageandcallsaconstructor.Therearetwoconstructorsinallstandardexceptions;

Thefirstisthedefaultconstructor,andthesecondtakesastringargumentsoyoucanplacepertinentinformationintheexception:

thrownewNullPointerException("

t=null"

);

Thisstringcanlaterbeextractedusingvariousmethods,asyou’llsee.

Thekeywordthrowcausesanumberofrelativelymagicalthingstohappen.Typically,you’llfirstusenewtocreateanobjectthatrepresentstheerrorcondition.Yougivetheresultingreferencetothrow.Theobjectis,ineffect,“returned”fromthemethod,eventhoughthatobjecttypeisn’tnormallywhatthemethodisdesignedtoreturn.Asimplisticwaytothinkaboutexceptionhandlingisasadifferentkindofreturnmechanism,althoughyougetintotroubleifyoutakethatanalogytoofar.Youcanalsoexitfromordinaryscopesbythrowinganexception.Butavalueisreturned,andthemethodorscopeexits.

Anysimilaritytoanordinaryreturnfromamethodendshere,becausewhereyoureturnissomeplacecompletelydifferentfromwhereyoureturnforanormalmethodcall.(Youendupinanappropriateexceptionhandlerthatmightbefar—manylevelsawayonthecallstack—fromwheretheexceptionwasthrown.)

Inaddition,youcanthrowanytypeofThrowable(theexceptionrootclass)objectthatyouwant.Typically,you’llthrowadifferentclassofexceptionforeachdifferenttypeoferror.Theinformationabouttheerrorisrepresentedbothinsidetheexceptionobjectandimplicitlyinthenameoftheexceptionclass,sosomeoneinthebiggercontextcanfigureoutwhattodowithyourexception.(Often,theonlyinformationisthetypeofexception,andnothingmeaningfulisstoredwithintheexceptionobject.)

2.Catchinganexception

3

Ifamethodthrowsanexception,itmustassumethatexceptionwillbe“caught”anddealtwith.Oneoftheadvantagesofexceptionhandlingisthatitallowsyoutoconcentrateontheproblemyou’retryingtosolveinoneplace,andthendealwiththeerrorsfromthatcodeinanotherplace.

Toseehowanexceptioniscaught,youmustfirstunderstandtheconceptofaguardedregion.Thisisasectionofcodethatmightproduceexceptionsandisfollowedbythecodetohandlethoseexceptions.

2.1Thetryblock

Ifyou’reinsideamethodandyouthrowanexception(oranothermethodyoucallwithinthismethodthrowsanexception),thatmethodwillexitintheprocessofthrowing.Ifyoudon’twantathrowtoexitthemethod,youcanset

upaspecialblockwithinthatmethodtocapturetheexception.Thisiscalledthetryblockbecauseyou“try”yourvariousmethodcallsthere.Thetryblock

isanordinaryscopeprecededbythekeywordtry:

try{

//Codethatmightgenerateexceptions

}

Ifyouwerecheckingforerrorscarefullyinaprogramminglanguagethatdidn’tsupportexceptionhandling,you’dhavetosurroundeverymethodcallwithsetupanderrortestingcode,evenifyoucallthesamemethodseveraltimes.Withexceptionhandling,youputeverythinginatryblockandcapturealltheexceptionsinoneplace.Thismeansyourcodeismucheasiertowriteandreadbecausethegoalofthecodeisnotconfusedwiththeerrorchecking.

2.2Exceptionhandlers

Ofcourse,thethrownexceptionmustendupsomeplace.This“place”istheexceptionhandler,andthere’soneforeveryexceptiontypeyouwanttocatch.Exceptionhandlersimmediatelyfollowthetryblockandaredenotedbythekeywordcatch:

}catch(Type1id1){

//HandleexceptionsofType1

}catch(Type2id2){

//HandleexceptionsofType2

}catch(Type3id3){

4

//HandleexceptionsofType3

//etc...

Eachcatchclause(exceptionhandler)islikealittlemethodthattakesoneandonlyoneargumentofaparticulartype.Theidentifier(id1,id2,andsoon)canbeusedinsidethehandler,justlikeamethodargument.Sometimesyouneverusetheidentifierbecausethetypeoftheexceptiongivesyouenoughinformationtodealwiththeexception,buttheidentifiermuststillbethere.

Thehandlersmustappeardirectlyafter

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 冶金矿山地质

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2