关于异常的错误,朋友帮我解释下啊

yasu1984 2007-07-27 06:59:20
//: c10:E11_OnOffSwitch.java
//+M java E11_OnOffSwitch
/****************** Exercise 11 *****************
* Show that OnOffSwitch.java can fail by
* throwing a RuntimeException inside the try
* block.
***********************************************/
class Switch {
boolean status = false ;
boolean read() {
return status;
}
public void on() {
status = true;
}
public void off() {
status = false ;
}
public String toString() {
return "Switch = "
+ (status ? "Switch = on" : "Switch = off");
}
public void f() throws RuntimeException {
throw new RuntimeException("Inside try ");
}
}

class OnOffException1 extends Exception {}
class OnOffException2 extends Exception {}

public class E11_OnOffSwitch {
public static void main(String[] args) {
Switch sw = new Switch();
try {
try {
sw.on();
sw.f();
sw.off();
}catch(OnOffException1 e1) { //这里报错“
System.err.println("caught " + e1);
sw.off();
}catch(OnOffException2 e2) {//这里报错
System.err.println("caught " + e2);
sw.off();
}
}catch (RuntimeException e3) {
System.out.println("Oops! the exception '"
+ e3 + "' slipped through without "
+ "turning the switch off!");
}
}
}
...全文
113 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qnjian 2007-07-27
  • 打赏
  • 举报
回复
兄弟,应该把错误提示贴上来呀!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧