仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1363|回复: 19
打印 上一主题 下一主题

[学习教程] JAVA网页编程之Flow control and exception Handling

[复制链接]
透明 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:55:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
专门做了这个例子;而java的这个例子好像就是为了教学而写的,很多教学目的的例子是不考虑优化、性能的。2)FlowcontrolandexceptionHandling
Objective1)
Writecodeusingifandswitchstatementsandidentifylegalargumenttypesforthesestatements.

・Unreachablestatementsproduceacompile-timeerror.
while(false){x=3;}//won’tcompile
for(;false;){x=3;}//won’tcompile
if(false){x=3;}//willcompile,toprovidetheabilitytoconditionallycompilethecode.
while(1==2){…}//fine
・Alocalvariableinablockmaybere-declaredinanotherlocalblock,iftheblocksaredisjoint.

・iftakesabooleanarguments.Parenthesisrequired.elsepartisoptional.elseifstructureprovidesmultipleselectivebranching.
・switchtakesanargumentofbyte,short,charorint.(assignmentcompatibletoint)
・casevalueshouldbeaconstantexpressionthatcanbeevaluatedatcompiletime.
・Compilercheckseachcasevalueagainsttherangeoftheswitchexpression’sdatatype.Thefollowingcodewon’tcompile.
byteb;
switch(b){
case200://200notinrangeofbyte
default:
}
・Weneedtoplaceabreakstatementineachcaseblocktopreventtheexecutiontofallthroughothercaseblocks.Butthisisnotapartofswitchstatementandnotenforcedbythecompiler.
・defaultcasecanbeplacedanywhere.It’llbeexecutedonlyifnoneofthecasevaluesmatch.
・switchcanbenested.Nestedcaselabelsareindependent,don’tclashwithoutercaselabels.
・Emptyswitchconstructisavalidconstruct.Butanystatementwithintheswitchblockshouldcomeunderacaselabelorthedefaultcaselabel.

Objective2)
Writecodeusingallformsofloopsincludinglabeledandunlabeleduseofbreakandcontinueandstatethevaluestakenbyloopcountervariablesduringandafterloopexecution.

・3constructsCfor,while,do
・Allloopsarecontrolledbyabooleanexpression.
・Inwhileandfor,thetestoccursatthetop.Indo,testoccursatthebottom,sothebodyisexecutedatleastonce.
・Infor,wecandeclaremultiplevariablesinthefirstpartoftheloopseparatedbycommas,alsowecanhavemultiplestatementsinthethirdpartseparatedbycommas.
・Inthefirstsectionofforstatement,wecanhavealistofdeclarationstatementsoralistofexpressionstatements,butnotboth.Wecannotmixthem.
・Allexpressionsinthethirdsectionofforstatementwillalwaysexecute,evenifthefirstexpressionmakestheloopconditionfalse.ThereisnoshortCcircuithere.

・breakstatementcanbeusedwithanykindoflooporaswitchstatementorjustalabeledblock.
・continuestatementcanbeusedwithonlyaloop(anykindofloop).
・Loopscanhavelabels.Thebreakstatementabandonsprocessingofthecurrentloopentirely,thecontinuestatementonlyabandonsthecurrentlyprocessingtimearoundtheloop.
・Namesofthelabelsfollowthesamerulesasthenameofthevariables.(Identifiers)
・Labelscanhavethesamename,aslongastheydon’tencloseoneanother.
・Thereisnorestrictionagainstusingthesameidentifierasalabelandasthenameofapackage,class,interface,method,field,parameter,orlocalvariable.
・Infact,labelsmaybeappliedtoanystatements,buttheyareonlyusefulinthecontextofbeakandcontinueinloopconstructions.

Objective3)
Writecodethatmakesproperuseofexceptionsandexceptionhandlingclauses(trycatchfinally)anddeclaresmethodsandoverridingmethodsthatthrowexceptions.
・Inanymethodorconstructor(notclass)thatcontainslinesthatmightthrowacheckedexception,youmusteitherhandletheexceptionusingatry/catchconstruct,ordeclarethatthemethodthrowstheexception.Donotneedboth,butcompilerwouldn’tcompainthat.
・Java.lang.RuntimeExceptionandjava.lang.Errorneednotbehandledordeclared.
・UsethrownewxxxException()tothrowanexceptionexplicitly.Ifthethrownobjectisnull,aNullPointerExceptionwillbethrownatthehandler.
・IfamethodA()isdeclaredasthrowinganExceptionbythethrowsclause,whenyoucallA()inmethodB(),methodB()hastoeitherthrowsexceptionorcatchtheexceptionbyA().

・Anexceptioncausesajumptotheendoftryblock.Iftheexceptionoccurredinamethodcalledfromatryblock,thecalledmethodisabandoned.
・Ifthere’sacatchblockfortheoccurredexceptionoraparentclassoftheexception,theexceptionisnowconsideredhandled.
・Thetry/catchclausemusttraperrorsintheordertheirnaturalorderofhierarchy.
・Atleastone‘catch’blockorone‘finally’blockmustaccompanya‘try’statement.Ifall3blocksarepresent,theorderisimportant.(try/catch/finally)
・finallyandcatchcancomeonlywithtry,theycannotappearontheirown.
・Regardlessofwhetherornotanexceptionoccurredorwhetherornotitwashandled,ifthereisafinallyblock,it’llbeexecutedalways.(Evenifthereisareturnstatementintryblock).
・System.exit()anderrorconditionsaretheonlyexceptionswherefinallyblockisnotexecuted.
・Iftherewasnoexceptionortheexceptionwashandled,executioncontinuesatthestatementafterthetry/catch/finallyblocks.
・Iftheexceptionisnothandled,theprocessrepeatslookingfornextenclosingtryblockupthecallhierarchy.Ifthissearchreachesthetoplevelofthehierarchy(thepointatwhichthethreadwascreated),thenthethreadiskilledandmessagestacktraceisdumpedtoSystem.err.
・Ifanexceptionhandlerre-throwsanexception(throwinacatchblock),samerulesapply.Eitheryouneedtohaveatry/catchwithinthecatchorspecifytheentiremethodasthrowingtheexceptionthat’sbeingre-throwninthecatchblock.CatchblocksatthesamelevelwillnothandletheexceptionsthrowninacatchblockCitneedsitsownhandlers.
・Ifthere’snocodeintryblockthatmaythrowexceptionsspecifiedinthecatchblocks,compilerwillproduceanerror.(Thisisnotthecaseforsuper-classException)
・Inotherwords,anoverridingmethodmaynotthrowcheckedexceptionsthatarenotthrownbytheoverriddenmethod.AmethodcanreturnanException(?)








Hereistheexceptionhierarchy.

Object
|
|
Throwable
||
||
||
|Error
|
|
Exception-->ClassNotFoundException,
ClassNotSupportedException,
IllegalAccessException,
InstantiationException,
InterruptedException,
NoSuchMethodException,
RuntimeException,-----àEmptyStackException,
AWTException,NoSuchElementException,
IOExceptionArithmeticException,
ArrayStoreException,
ClassCastException,
IllegalArgumentException,---àIllegalThreadStateException
IllegalMonitorStateException,NumberFormatException
IndexOutOfBoundsException,
NegativeArraySizeException,
NullPointerException,
SecurityException.

IndexOutOfBoundsException-->ArrayIndexOutOfBoundsException,StringIndexOutOfBoundsException

IOException-->EOFException,
FileNotFoundException,
InterruptedIOException,
UTFDataFormatException,
MalformedURLException,
ProtocolException,
SockException,
UnknownHostException,
UnknownServiceException.



你总不能说你写框架吧,那无疑会加大工作量,现在大多企业采取的是折中的办法,就是改别人写好的框架,可要改框架,前提是你对这个框架足够的了解,这就更难了。
再见西城 该用户已被删除
沙发
发表于 2015-1-21 17:04:09 | 只看该作者
你快去找一份Java的编程工作来做吧(如果是在校学生可以去做兼职啊),在实践中提高自己,那才是最快的。不过你得祈祷在公司里碰到一个高手,而且他 还愿意不厌其烦地教你,这样好象有点难哦!还有一个办法就是读开放源码的程序了。我们知道开放源码大都出自高手,他们设计合理,考虑周到,再加上有广大的程序员参与,代码的价值自然是字字珠叽,铿锵有力(对不起,偶最近《金装四大才子》看多了)。
谁可相欹 该用户已被删除
板凳
发表于 2015-1-24 05:58:36 | 只看该作者
是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能
海妖 该用户已被删除
地板
发表于 2015-1-24 20:35:19 | 只看该作者
应用在电视机、电话、闹钟、烤面包机等家用电器的控制和通信。由于这些智能化家电的市场需求没有预期的高,Sun公司放弃了该项计划。随着1990年代互联网的发展
爱飞 该用户已被删除
5#
发表于 2015-2-2 13:03:16 | 只看该作者
我大二,Java也只学了一年,觉得还是看thinking in java好,有能力的话看英文原版(中文版翻的不怎么好),还能提高英文文档阅读能力。
小女巫 该用户已被删除
6#
发表于 2015-2-7 20:16:12 | 只看该作者
不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。
飘飘悠悠 该用户已被删除
7#
发表于 2015-2-23 09:51:11 | 只看该作者
我大二,Java也只学了一年,觉得还是看thinking in java好,有能力的话看英文原版(中文版翻的不怎么好),还能提高英文文档阅读能力。
再现理想 该用户已被删除
8#
发表于 2015-3-7 00:28:37 | 只看该作者
另外编写和运行Java程序需要JDK(包括JRE),在sun的官方网站上有下载,thinking in java第三版用的JDK版本是1.4,现在流行的版本1.5(sun称作J2SE 5.0,汗),不过听说Bruce的TIJ第四版国外已经出来了,是专门为J2SE 5.0而写的。
山那边是海 该用户已被删除
9#
发表于 2015-3-8 18:42:12 | 只看该作者
是一种使网页(Web Page)产生生动活泼画面的语言
admin 该用户已被删除
10#
发表于 2015-3-16 04:55:40 | 只看该作者
Java 不同于一般的编译执行计算机语言和解释执行计算机语言。它首先将源代码编译成二进制字节码(bytecode),然后依赖各种不同平台上的虚拟机来解释执行字节码。从而实现了“一次编译、到处执行”的跨平台特性。
透明 该用户已被删除
11#
 楼主| 发表于 2015-3-16 06:33:33 | 只看该作者
Java是一个纯的面向对象的程序设计语言,它继承了 C++语言面向对象技术的核心。Java舍弃了C ++语言中容易引起错误的指针(以引用取代)、运算符重载(operator overloading)
简单生活 该用户已被删除
12#
发表于 2015-3-17 00:10:23 | 只看该作者
J2SE开发桌面应用软件比起 VC,VB,DEPHI这些传统开发语言来说,优势好象并不明显。J2ME对于初学者来说,好象又有点深奥,而且一般开发者很难有开发环境。
柔情似水 该用户已被删除
13#
发表于 2015-3-20 10:28:10 | 只看该作者
你就该学一学Servlet了。Servlet就是服务器端小程序,他负责生成发送给客户端的HTML文件。JSP在执行时,也是先转换成Servlet再运行的。虽说JSP理论上可以完全取代Servlet,这也是SUN推出JSP的本意,可是Servlet用来控制流程跳转还是挺方便的,也令程序更清晰。接下来你应该学习一下Javabean了,可能你早就看不管JSP在HTML中嵌Java代码的混乱方式了,这种方式跟ASP又有什么区别呢?
小妖女 该用户已被删除
14#
发表于 2015-4-1 21:10:14 | 只看该作者
是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言
老尸 该用户已被删除
15#
发表于 2015-4-6 20:11:07 | 只看该作者
是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能
蒙在股里 该用户已被删除
16#
发表于 2015-4-10 03:31:11 | 只看该作者
一直感觉JAVA很大,很杂,找不到学习方向,前两天在网上找到了这篇文章,感觉不错,给没有方向的我指了一个方向,先不管对不对,做下来再说。
金色的骷髅 该用户已被删除
17#
发表于 2015-4-16 00:10:22 | 只看该作者
应用在电视机、电话、闹钟、烤面包机等家用电器的控制和通信。由于这些智能化家电的市场需求没有预期的高,Sun公司放弃了该项计划。随着1990年代互联网的发展
18#
发表于 2015-4-21 23:12:43 | 只看该作者
我大二,Java也只学了一年,觉得还是看thinking in java好,有能力的话看英文原版(中文版翻的不怎么好),还能提高英文文档阅读能力。
只想知道 该用户已被删除
19#
发表于 2015-5-1 02:10:17 | 只看该作者
其实说这种话的人就如当年小日本号称“三个月拿下中国”一样大言不惭。不是Tomjava泼你冷水,你现在只是学到了Java的骨架,却还没有学到Java的精髓。接下来你得研究设计模式了。
莫相离 该用户已被删除
20#
发表于 2015-6-10 15:40:46 | 只看该作者
你现在最缺的是实际的工作经验,而不是书本上那些凭空想出来的程序。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-5 14:52

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表