不建议jsp原理一点都不懂就直接去做项目。弄不出来反倒影响积极情绪。 现在网络上流传的视频教学有张孝祥,孙鑫,翁恺三个版本。电驴里可能找的到。可以都下载下来看看。刚接触java看看张孝祥的,莫莫唧唧讲得挺细的。学了一遍再看看翁恺的。 jsp就是j2ee的。感觉java基础差不多就该去抠Java Server Page了
第一个版本会打印出标准错误,第二个则打印出我们的选择流程。若在Windows下工作,就不能重定向标准错误。因此,我们一般愿意使用第二个版本,并将结果送给System.out;这样一来,输出就可重定向到我们希望的任何路径。 除此以外,我们还可从Throwable的基础类Object(所有对象的基础类型)获得另外一些方法。对于违例控制来说,其中一个可能有用的是getClass(),它的作用是返回一个对象,用它代表这个对象的类。我们可依次用getName()或toString()查询这个Class类的名字。亦可对Class对象进行一些复杂的操作,尽管那些操作在违例控制中是不必要的。本章稍后还会详细讲述Class对象。 下面是一个特殊的例子,它展示了Exception方法的使用(若执行该程序遇到困难,请参考第3章3.1.2小节“赋值”): //: ExceptionMethods.java // Demonstrating the Exception Methods package c09;public class ExceptionMethods { public static void main(String[] args) { try { throw new Exception("Here's my Exception"); } catch(Exception e) { System.out.println("Caught Exception"); System.out.println( "e.getMessage(): " + e.getMessage()); System.out.println( "e.toString(): " + e.toString()); System.out.println("e.printStackTrace():"); e.printStackTrace(); } } } ///:~该程序输出如下:Caught Exception e.getMessage(): Here's my Exception e.toString(): java.lang.Exception: Here's my Exception e.printStackTrace(): java.lang.Exception: Here's my Exception at ExceptionMethods.main可以看到,该方法连续提供了大量信息——每类信息都是前一类信息的一个子集。 -------------- catch(Exception e) { System.out.println("一个违例已经产生"); throw e; } ------------ //: Rethrowing.java // Demonstrating fillInStackTrace()public class Rethrowing { public static void f() throws Exception { System.out.println( "originating the exception in f()"); throw new Exception("thrown from f()"); } public static void g() throws Throwable { try { f(); } catch(Exception e) { System.out.println( "Inside g(), e.printStackTrace()"); e.printStackTrace(); throw e; // 17 // throw e.fillInStackTrace(); // 18 } } public static void main(String[] args) throws Throwable { try { g(); } catch(Exception e) { System.out.println( "Caught in main, e.printStackTrace()"); e.printStackTrace(); } } } ///:~ --------------- originating the exception in f() Inside g(), e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.f(Rethrowing.java:8) at Rethrowing.g(Rethrowing.java:12) at Rethrowing.main(Rethrowing.java:24) Caught in main, e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.f(Rethrowing.java:8) at Rethrowing.g(Rethrowing.java:12) at Rethrowing.main(Rethrowing.java:24) --------------
若将第17行标注(变成注释行),而撤消对第18行的标注,就会换用fillInStackTrace(),结果如下:originating the exception in f() Inside g(), e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.f(Rethrowing.java:8) at Rethrowing.g(Rethrowing.java:12) at Rethrowing.main(Rethrowing.java:24) Caught in main, e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.g(Rethrowing.java:18) at Rethrowing.main(Rethrowing.java:24) ------------------------ //: ThrowOut.java public class ThrowOut { public static void main(String[] args) throws Throwable { try { throw new Throwable(); } catch(Exception e) { System.out.println("Caught in main()"); } } } ///:~ ---------------- //: RethrowNew.java // Rethrow a different object from the one that // was caughtpublic class RethrowNew { public static void f() throws Exception { System.out.println( "originating the exception in f()"); throw new Exception("thrown from f()"); } public static void main(String[] args) { try { f(); } catch(Exception e) { System.out.println( "Caught in main, e.printStackTrace()"); e.printStackTrace(); throw new NullPointerException("from main"); } } } ///:~输出如下:originating the exception in f() Caught in main, e.printStackTrace() java.lang.Exception: thrown from f() at RethrowNew.f(RethrowNew.java:8) at RethrowNew.main(RethrowNew.java:13) java.lang.NullPointerException: from main at RethrowNew.main(RethrowNew.java:18) --------------- //: NeverCaught.java // Ignoring RuntimeExceptionspublic class NeverCaught { static void f() { throw new RuntimeException("From f()"); } static void g() { f(); } public static void main(String[] args) { g(); } } ///:~大家已经看到,一个RuntimeException(或者从它继承的任何东西)属于一种特殊情况,因为编译器不要求为这些类型指定违例规范。 输出如下:java.lang.RuntimeException: From f() at NeverCaught.f(NeverCaught.java:9) at NeverCaught.g(NeverCaught.java:12) at NeverCaught.main(NeverCaught.java:15) ---------- //: Inheriting.java // Inheriting your own exceptionsclass MyException extends Exception { public MyException() {} public MyException(String msg) { super(msg); } }public class Inheriting { public static void f() throws MyException { System.out.println( "Throwing MyException from f()"); throw new MyException(); } public static void g() throws MyException { System.out.println( "Throwing MyException from g()"); throw new MyException("Originated in g()"); } public static void main(String[] args) { try { f(); } catch(MyException e) { e.printStackTrace(); } try { g(); } catch(MyException e) { e.printStackTrace(); } } } ///:~继承在创建新类时发生:class MyException extends Exception { public MyException() {} public MyException(String msg) { super(msg); } } --------------- Throwing MyException from f() MyException at Inheriting.f(Inheriting.java:16) at Inheriting.main(Inheriting.java:24) Throwing MyException from g() MyException: Originated in g() at Inheriting.g(Inheriting.java:20) at Inheriting.main(Inheriting.java:29) ----------- //: Inheriting2.java // Inheriting your own exceptionsclass MyException2 extends Exception { public MyException2() {} public MyException2(String msg) { super(msg); } public MyException2(String msg, int x) { super(msg); i = x; } public int val() { return i; } private int i; }public class Inheriting2 { public static void f() throws MyException2 { System.out.println( "Throwing MyException2 from f()"); throw new MyException2(); } public static void g() throws MyException2 { System.out.println( "Throwing MyException2 from g()"); throw new MyException2("Originated in g()"); } public static void h() throws MyException2 { System.out.println( "Throwing MyException2 from h()"); throw new MyException2( "Originated in h()", 47); } public static void main(String[] args) { try { f(); } catch(MyException2 e) { e.printStackTrace(); } try { g(); } catch(MyException2 e) { e.printStackTrace(); } try { h(); } catch(MyException2 e) { e.printStackTrace(); System.out.println("e.val() = " + e.val()); } } } ///:~ ------------------ Throwing MyException2 from f() MyException2 at Inheriting2.f(Inheriting2.java:22) at Inheriting2.main(Inheriting2.java:34) Throwing MyException2 from g() MyException2: Originated in g() at Inheriting2.g(Inheriting2.java:26) at Inheriting2.main(Inheriting2.java:39) Throwing MyException2 from h() MyException2: Originated in h() at Inheriting2.h(Inheriting2.java:30) at Inheriting2.main(Inheriting2.java:44) e.val() = 47
try { // 要保卫的区域: // 可能“掷”出A,B,或C的危险情况 } catch (A a1) { // 控制器 A } catch (B b1) { // 控制器 B } catch (C c1) { // 控制器 C } finally { // 每次都会发生的情况 } --------- //: FinallyWorks.java // The finally clause is always executedpublic class FinallyWorks { static int count = 0; public static void main(String[] args) { while(true) { try { // post-increment is zero first time: if(count++ == 0) throw new Exception(); System.out.println("No exception"); } catch(Exception e) { System.out.println("Exception thrown"); } finally { System.out.println("in finally clause"); if(count == 2) break; // out of "while" } } } } ///:~ --------- Exception thrown in finally clause No exception in finally clause ---------- //: OnOffSwitch.java // Why use finally?class Switch { boolean state = false; boolean read() { return state; } void on() { state = true; } void off() { state = false; } }public class OnOffSwitch { static Switch sw = new Switch(); public static void main(String[] args) { try { sw.on(); // Code that can throw exceptions... sw.off(); } catch(NullPointerException e) { System.out.println("NullPointerException"); sw.off(); } catch(IllegalArgumentException e) { System.out.println("IOException"); sw.off(); } } } ///:~ -------------- //: WithFinally.java // Finally Guarantees cleanupclass Switch2 { boolean state = false; boolean read() { return state; } void on() { state = true; } void off() { state = false; } }public class WithFinally { static Switch2 sw = new Switch2(); public static void main(String[] args) { try { sw.on(); // Code that can throw exceptions... } catch(NullPointerException e) { System.out.println("NullPointerException"); } catch(IllegalArgumentException e) { System.out.println("IOException"); } finally { sw.off(); } } } ///:~ ----------------------- //: AlwaysFinally.java // Finally is always executedclass Ex extends Exception {}public class AlwaysFinally { public static void main(String[] args) { System.out.println( "Entering first try block"); try { System.out.println( "Entering second try block"); try { throw new Ex(); } finally { System.out.println( "finally in 2nd try block"); } } catch(Ex e) { System.out.println( "Caught Ex in first try block"); } finally { System.out.println( "finally in 1st try block"); } } } ///:~ --------------- Entering first try block Entering second try block finally in 2nd try block Caught Ex in first try block finally in 1st try block -------------- //: LostMessage.java // How an exception can be lostclass VeryImportantException extends Exception { public String toString() { return "A very important exception!"; } }class HoHumException extends Exception { public String toString() { return "A trivial exception"; } }public class LostMessage { void f() throws VeryImportantException { throw new VeryImportantException(); } void dispose() throws HoHumException { throw new HoHumException(); } public static void main(String[] args) throws Exception { LostMessage lm = new LostMessage(); try { lm.f(); } finally { lm.dispose(); } } } ///:~输出如下:A trivial exception at LostMessage.dispose(LostMessage.java:21) at LostMessage.main(LostMessage.java:29)
java基础打好了,就该上层建筑了啊。jsp肯定要看的啊。
建议找点项目做吧,这样知道自己该学什么
要学的东西太多le
看你要用那方面的了
现在网络上流传的视频教学有张孝祥,孙鑫,翁恺三个版本。电驴里可能找的到。可以都下载下来看看。刚接触java看看张孝祥的,莫莫唧唧讲得挺细的。学了一遍再看看翁恺的。
jsp就是j2ee的。感觉java基础差不多就该去抠Java Server Page了
1 要学先选一个方向,因为Java也分很多类别的,如J2SE,J2EE,J2ME,EJB等等,还有各种框架:Struts, JSF,Spring, Hiberaate.
2. 要学就要学精些。
3. 要实践,只有在真正做项目的过程才能提高。
总之学习学习再学习当然中途去做做项目是最好的了
那是因为没有学到Java的精髓。后来找个培训机构考了SCJP,现在写JSP或者做J2EE基本没什么问题了。关键是基础。
加油啊,每一步要走塌实了~
再考虑学些筐架。
却不 知道 怎样去 些,该这么办啊.各位能推荐几本好书看看吗?
看了这个
很有收获
希望对楼主有所帮助
http://daxing.bokee.com/2265541.html
===========================
(转帖):
J2ee学习流程 在这里我谈谈我在学习j2ee流程,并谈到在此过程中领会的经验和教训。以便后来者少走弯路。
Java发展到现在,按应用来分主要分为三大块:J2SE,J2ME和J2EE。这三块相互补充,应用范围不同。
J2SE就是Java2的标准版,主要用于桌面应用软件的编程;
J2ME主要应用于嵌入是系统开发,如手机和PDA的编程;
J2EE是Java2的企业版,主要用于分布式的网络程序的开发,如电子商务网站和ERP系统。 1)先学习j2se
要学习j2ee就要先学习j2se,刚开始学习j2se先建议不要使用IDE,然后渐渐的过渡到使用IDE开发,毕竟用它方便嘛。学习j2se推荐三本书,《java2核心技术一二卷》,《java编程思想》,《java模式》。其中《java编程思想》要研读,精读。这一段时间是基本功学习,时间会很长,也可能很短,这要看学习者自身水平而定。 不要被IDE纠缠
在学习java和j2ee过程中,你会遇到五花八门的IDE,不要被他们迷惑,学JAVA的时候,要学语言本身的东西,不要太在意IDE的附加功能,JAVA编程在不同IDE之间的转换是很容易的,过于的在意IDE的功能反而容易耽误对语言本身的理解。目前流行的IDE有jbuilder,eclipse和eclipse的加强版WSAD。用好其中一个就可以了,推荐从eclipse入手j2ee。因为Jbuilder更适合于写j2se程序。 2)选择和学习服务器使用配置
当你有了j2se和IDE的经验时,可以开始j2ee的学习了,web服务器:tomcat,勿庸置疑,tomcat为学习web服务首选。而应用服务器目前主要有三个:jboss、weblogic、websphere。有很多项目开始采用jboss,并且有大量的公司开始做websphere或weblogic向jboss应用服务器的移植(节省成本),这里要说的是,学习tomcat和jboss我认为是首选,也是最容易上手的。学习服务器使用配置最好去询问有经验的人(有条件的话),因为他们或许一句话就能解决问题,你自己上网摸索可能要一两天(我就干过这种傻事),我们应该把主要时间放在学习原理和理论上,一项特定技术的使用永远代替不了一个人的知识和学问。 3)学习web知识
如果你是在做电子商务网站等时,你可能要充当几个角色,这是你还要学习:
html,可能要用到dreamwave等IDE。
Javascript,学会简单的数据校验,数据联动显示等等 4)J2eeAPI学习
学习j2eeAPI和学习服务器应该是一个迭代的过程。
先学习jsp和servlet编程,这方面的书很多,我建立看oreilly公司的两本《jsp设计》和《java servlet编程》,oreilly出的书总是那本优秀,不得不佩服。
学习jdbc数据库编程,j2ee项目大多都是MIS系统,访问数据库是核心。这本应属于j2se学习中,这里拿出来强调一下。
学习jndi api,它和学习ejb可以结合起来。
学习ejb api,推荐书《精通ejb》
经过上面的这些的学习,大概可以对付一般的应用了。
另外一种学习方法:跟着sun公司的《j2ee tutorial》一路学下来,j2ee-tutorial是很不错的入门教材。 5)学习ejb设计模式和看代码(最重要)
设计模式是练内功,其重要性可以这么说吧,如果你不会用设计模式的话,你将写出一堆使用了ejb的垃圾,有慢又是一堆bug,其结果不如不用ejb实现(ejb不等于j2ee)
无论学习什么语言,都应该看大量代码,你看的代码量不到一定数量,是学不好j2ee的,另外看代码也是一个学习别人良好编程习惯和规范的好方法,有时候你意识不到自己的代码写的有多烂,:) 目前有很多开源的工程可以作为教材:
jive论坛
petstore sun公司
dune sun公司
等等,研读一个,并把它用到自己的工程中来。 6)J2ee其他学习
当你渐渐对j2ee了解到一定深度时,你要开始关注当前领域中的一些技术变化,J2ee是一块百家争鸣的领域,大家都在这里提出自己的解决方案,例如struts,hibernate,ofbiz等等,学习这些东西要你的项目和目标而定,预先补充一下未尝不可,但不用涉及太深,毕竟学习原理和理论是最最重要的事。 7)利用网络资源
java手册 http://www.javaalmanac.com/ 极好的一个在线帮助
开源网站 http://www.sourceforge.net/
J2EE讨论 http://www.theserverside.com/
www.javaworld.com
www.java.sun.com 8)学习极限编程
J2EE项目是多人合作的项目,也是调试很困难的一类项目。如果没有良好的项目规划是不能
做好J2EE项目的,了解和项目开发管理相关的知识也有必要
项目管理:Ant,类似于C语言中make的作用
版本管理:CVS等,例如wincvs,tortoise cvs
单元测试:JUnit,摆脱插入main到类中进行调试的原始方法
压力测试:JMeter,开源工程,简单,易用
目前常见j2eeAPI
JavaServer Pages(JSP)技术1.2
Java Servlet技术2.3
JDBC API 2.0
Java XML处理API(JAXP)1.1
Enterprise JavaBeans技术2.0
Java消息服务(JMS)1.0
Java命名目录接口(JNDI)1.2
Java事务API(JTA) 1.0
JavaMail API 1.2
JavaBeans激活架构(JAF)1.0
J2EE连接器体系结构(JCA)1.0
Java认证和授权服务(JAAS)1.0
学习上面的某些API要以你的项目而定,了解所有他们总之是有好处的
上面印证了大家说的一句话,java语言本身不难学,但是技术太多,所以学java很费劲。回想一下,基本上每个初学者,在刚学习java的时候可能都会问别人这么一句话,你怎么知道的哪个方法(api)在哪个包里的?呵呵,无他,唯手熟尔。9)JAVA相关软件
* Jakarta - jakarta.apache.org
* Sourceforge - www.sourceforge.net
* Java Boutique - javaboutique.internet.com
* Giant Java Tree - www.git.org
* GUN java programs - www.gnu.org/software
[htm]http://www.juju365.com/news/index.asp?flag=1&newstag=JAVA&offset=0[/htm]
如果觉得累好好考虑考虑自己是否喜欢java如果你只是喜欢java的名气 很难有所成就的
除此以外,我们还可从Throwable的基础类Object(所有对象的基础类型)获得另外一些方法。对于违例控制来说,其中一个可能有用的是getClass(),它的作用是返回一个对象,用它代表这个对象的类。我们可依次用getName()或toString()查询这个Class类的名字。亦可对Class对象进行一些复杂的操作,尽管那些操作在违例控制中是不必要的。本章稍后还会详细讲述Class对象。
下面是一个特殊的例子,它展示了Exception方法的使用(若执行该程序遇到困难,请参考第3章3.1.2小节“赋值”):
//: ExceptionMethods.java
// Demonstrating the Exception Methods
package c09;public class ExceptionMethods {
public static void main(String[] args) {
try {
throw new Exception("Here's my Exception");
} catch(Exception e) {
System.out.println("Caught Exception");
System.out.println(
"e.getMessage(): " + e.getMessage());
System.out.println(
"e.toString(): " + e.toString());
System.out.println("e.printStackTrace():");
e.printStackTrace();
}
}
} ///:~该程序输出如下:Caught Exception
e.getMessage(): Here's my Exception
e.toString(): java.lang.Exception: Here's my Exception
e.printStackTrace():
java.lang.Exception: Here's my Exception
at ExceptionMethods.main可以看到,该方法连续提供了大量信息——每类信息都是前一类信息的一个子集。
--------------
catch(Exception e) {
System.out.println("一个违例已经产生");
throw e;
}
------------
//: Rethrowing.java
// Demonstrating fillInStackTrace()public class Rethrowing {
public static void f() throws Exception {
System.out.println(
"originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void g() throws Throwable {
try {
f();
} catch(Exception e) {
System.out.println(
"Inside g(), e.printStackTrace()");
e.printStackTrace();
throw e; // 17
// throw e.fillInStackTrace(); // 18
}
}
public static void
main(String[] args) throws Throwable {
try {
g();
} catch(Exception e) {
System.out.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
} ///:~
---------------
originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)
--------------
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.g(Rethrowing.java:18)
at Rethrowing.main(Rethrowing.java:24)
------------------------
//: ThrowOut.java
public class ThrowOut {
public static void
main(String[] args) throws Throwable {
try {
throw new Throwable();
} catch(Exception e) {
System.out.println("Caught in main()");
}
}
} ///:~
----------------
//: RethrowNew.java
// Rethrow a different object from the one that
// was caughtpublic class RethrowNew {
public static void f() throws Exception {
System.out.println(
"originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void main(String[] args) {
try {
f();
} catch(Exception e) {
System.out.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
throw new NullPointerException("from main");
}
}
} ///:~输出如下:originating the exception in f()
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at RethrowNew.f(RethrowNew.java:8)
at RethrowNew.main(RethrowNew.java:13)
java.lang.NullPointerException: from main
at RethrowNew.main(RethrowNew.java:18)
---------------
//: NeverCaught.java
// Ignoring RuntimeExceptionspublic class NeverCaught {
static void f() {
throw new RuntimeException("From f()");
}
static void g() {
f();
}
public static void main(String[] args) {
g();
}
} ///:~大家已经看到,一个RuntimeException(或者从它继承的任何东西)属于一种特殊情况,因为编译器不要求为这些类型指定违例规范。
输出如下:java.lang.RuntimeException: From f()
at NeverCaught.f(NeverCaught.java:9)
at NeverCaught.g(NeverCaught.java:12)
at NeverCaught.main(NeverCaught.java:15)
----------
//: Inheriting.java
// Inheriting your own exceptionsclass MyException extends Exception {
public MyException() {}
public MyException(String msg) {
super(msg);
}
}public class Inheriting {
public static void f() throws MyException {
System.out.println(
"Throwing MyException from f()");
throw new MyException();
}
public static void g() throws MyException {
System.out.println(
"Throwing MyException from g()");
throw new MyException("Originated in g()");
}
public static void main(String[] args) {
try {
f();
} catch(MyException e) {
e.printStackTrace();
}
try {
g();
} catch(MyException e) {
e.printStackTrace();
}
}
} ///:~继承在创建新类时发生:class MyException extends Exception {
public MyException() {}
public MyException(String msg) {
super(msg);
}
}
---------------
Throwing MyException from f()
MyException
at Inheriting.f(Inheriting.java:16)
at Inheriting.main(Inheriting.java:24)
Throwing MyException from g()
MyException: Originated in g()
at Inheriting.g(Inheriting.java:20)
at Inheriting.main(Inheriting.java:29)
-----------
//: Inheriting2.java
// Inheriting your own exceptionsclass MyException2 extends Exception {
public MyException2() {}
public MyException2(String msg) {
super(msg);
}
public MyException2(String msg, int x) {
super(msg);
i = x;
}
public int val() { return i; }
private int i;
}public class Inheriting2 {
public static void f() throws MyException2 {
System.out.println(
"Throwing MyException2 from f()");
throw new MyException2();
}
public static void g() throws MyException2 {
System.out.println(
"Throwing MyException2 from g()");
throw new MyException2("Originated in g()");
}
public static void h() throws MyException2 {
System.out.println(
"Throwing MyException2 from h()");
throw new MyException2(
"Originated in h()", 47);
}
public static void main(String[] args) {
try {
f();
} catch(MyException2 e) {
e.printStackTrace();
}
try {
g();
} catch(MyException2 e) {
e.printStackTrace();
}
try {
h();
} catch(MyException2 e) {
e.printStackTrace();
System.out.println("e.val() = " + e.val());
}
}
} ///:~
------------------
Throwing MyException2 from f()
MyException2
at Inheriting2.f(Inheriting2.java:22)
at Inheriting2.main(Inheriting2.java:34)
Throwing MyException2 from g()
MyException2: Originated in g()
at Inheriting2.g(Inheriting2.java:26)
at Inheriting2.main(Inheriting2.java:39)
Throwing MyException2 from h()
MyException2: Originated in h()
at Inheriting2.h(Inheriting2.java:30)
at Inheriting2.main(Inheriting2.java:44)
e.val() = 47
// 要保卫的区域:
// 可能“掷”出A,B,或C的危险情况
} catch (A a1) {
// 控制器 A
} catch (B b1) {
// 控制器 B
} catch (C c1) {
// 控制器 C
} finally {
// 每次都会发生的情况
}
---------
//: FinallyWorks.java
// The finally clause is always executedpublic class FinallyWorks {
static int count = 0;
public static void main(String[] args) {
while(true) {
try {
// post-increment is zero first time:
if(count++ == 0)
throw new Exception();
System.out.println("No exception");
} catch(Exception e) {
System.out.println("Exception thrown");
} finally {
System.out.println("in finally clause");
if(count == 2) break; // out of "while"
}
}
}
} ///:~
---------
Exception thrown
in finally clause
No exception
in finally clause
----------
//: OnOffSwitch.java
// Why use finally?class Switch {
boolean state = false;
boolean read() { return state; }
void on() { state = true; }
void off() { state = false; }
}public class OnOffSwitch {
static Switch sw = new Switch();
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions...
sw.off();
} catch(NullPointerException e) {
System.out.println("NullPointerException");
sw.off();
} catch(IllegalArgumentException e) {
System.out.println("IOException");
sw.off();
}
}
} ///:~
--------------
//: WithFinally.java
// Finally Guarantees cleanupclass Switch2 {
boolean state = false;
boolean read() { return state; }
void on() { state = true; }
void off() { state = false; }
}public class WithFinally {
static Switch2 sw = new Switch2();
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions...
} catch(NullPointerException e) {
System.out.println("NullPointerException");
} catch(IllegalArgumentException e) {
System.out.println("IOException");
} finally {
sw.off();
}
}
} ///:~
-----------------------
//: AlwaysFinally.java
// Finally is always executedclass Ex extends Exception {}public class AlwaysFinally {
public static void main(String[] args) {
System.out.println(
"Entering first try block");
try {
System.out.println(
"Entering second try block");
try {
throw new Ex();
} finally {
System.out.println(
"finally in 2nd try block");
}
} catch(Ex e) {
System.out.println(
"Caught Ex in first try block");
} finally {
System.out.println(
"finally in 1st try block");
}
}
} ///:~
---------------
Entering first try block
Entering second try block
finally in 2nd try block
Caught Ex in first try block
finally in 1st try block
--------------
//: LostMessage.java
// How an exception can be lostclass VeryImportantException extends Exception {
public String toString() {
return "A very important exception!";
}
}class HoHumException extends Exception {
public String toString() {
return "A trivial exception";
}
}public class LostMessage {
void f() throws VeryImportantException {
throw new VeryImportantException();
}
void dispose() throws HoHumException {
throw new HoHumException();
}
public static void main(String[] args)
throws Exception {
LostMessage lm = new LostMessage();
try {
lm.f();
} finally {
lm.dispose();
}
}
} ///:~输出如下:A trivial exception
at LostMessage.dispose(LostMessage.java:21)
at LostMessage.main(LostMessage.java:29)
偶毕业三年多了。现在虚岁已经是27了。现在也喜欢JAVA。也在学。能不能介绍一下你的那位朋友的“事迹”呀。也许能从中找到些信心呢。
偶的email:[email protected]欢迎交流交流。
现在就把魔兽给删了!
( (..) ) you 成功!