package day05;
public class Demo7 {
static class Student {
private String name;
public Student(String name) {
this.name = name;
}
public int div(int a, int b) {
return a / b;
}
public String toString() {
return name;
}
}
public static void main(String[] args) {
Student s = new Student("张三");
try {
s.div(10, 0);
return;
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(s.toString());
s = null;
System.out.println("回收资源...");
}
}
}
package day05;
class LpException extends Exception {
public LpException() {
super("蓝屏");
}
public LpException(String msg) {
super(msg);
}
}
class MyException extends Exception {
public MyException() {
super("冒烟");
}
public MyException(String msg) {
super(msg);
}
}
class ProgressException extends Exception {
public ProgressException() {
super("影响课程进度");
}
public ProgressException(String msg) {
super(msg);
}
}
class Computer {
int state = 1;
public void run() throws LpException, MyException {
if (state == 2) {
throw new LpException("电脑蓝屏了");
} else if (state == 3) {
throw new MyException("电脑冒烟了");
} else {
System.out.println("电脑运行正常");
}
}
public void reset() {
state = 1;
System.out.println("电脑重启中...");
}
}
class Teacher {
private String name;
private Computer computer;
public Teacher(String name, Computer computer) {
this.name = name;
this.computer = computer;
}
public void teach() throws ProgressException {
try {
computer.run();
} catch (LpException e) {
System.out.println("电脑蓝屏");
computer.reset();
System.out.println(name + "老师,继续上课");
} catch (MyException e) {
System.out.println("电脑冒烟");
test();
System.out.println("学生上自习");
throw new ProgressException("上课进度受到了影响");
}
}
public void test() {
System.out.println("上自习");
}
}
public class Demo9 {
public static void main(String[] args) {
Computer computer = new Computer();
computer.state = 3;
Teacher teacher = new Teacher("张三", computer);
try {
teacher.teach();
} catch (Exception e) {
e.printStackTrace();
}
}
}