computer 版 (精华区)
发信人: arbo (arbo), 信区: program
标 题: java笔记3
发信站: 听涛站 (2001年09月17日10:43:28 星期一), 站内信件
发信人: airforce1 (五湖废人), 信区: Java
标 题: java笔记3
发信站: BBS 水木清华站 (Sat Apr 1 14:17:25 2000)
关于异常捕捉
一个例子
try{
开水龙头
关水龙头
}
catch (Exception e){
操作失败信息
}
finally{
不管水龙关有无成功,再关一次(总会被执行)
}
如果多曾2中某层程序发生异常,会一层层向上抛,到有捕捉为止。
函数后加异常
public void connectMe() throws Exception{
...
}
public void test{
...
try {
connectMe();
}
catch (Exception e){
...
}
//在test里必须要做try catch 操作,不然编译错。
一些JDBC里操作就是用的这种方法强制要用try catch来处理
create your own exception
example:
public class ServerTimedOutException extends Exception{
...
public SevertimedOutException (...){..}
public String getReason(){..}
public String getPort() {..}
}
To throw a exception that you have created use the syntax
throw new SeverTimedOutException(..);
注意, throw and throws is different,
public void test() throws Exception{
...
if (..) throw new Exception();
..
}
--
--
※ 来源:·听涛站 tingtao.dhs.org·[FROM: 匿名天使的家]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:0.835毫秒