关于异常的错误,朋友帮我解释下啊
//: 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!");
}
}
}